Cassandra 列表数据类型 C# 获取最后一个元素
Cassandra List Data Type C# get the last element
我在 Cassandra DB 中有一个列表。
我使用以下代码来获取它们,
string query = "select name from table where userid = 'abcd' ;";
try
{
Connector connector = new Connector();
session = connector.Connect();
RowSet rows = session.Execute(query);
if (rows.Columns.Length > 0)
{
foreach (Row row in rows)
{
data = row["name"].ToString();
}
status = true;
}
}
else
{
// no rows
}
session.Cluster.Shutdown();
}
catch (Exception ex)
{
}
我需要将这个正在转换为字符串的列表对象转换为数组,以便我可以根据需要获取最后一个字符串。
是否有任何内置方法可以做到这一点?
我最后用了下面的代码
foreach (Row row in rows)
{
object a = row["name"];
string[] arr = ((IEnumerable)a).Cast<object>()
.Select(x => x.ToString())
.ToArray();
int ab = arr.Length;
/*for all items*/
for(int i=0; i<ab;i++)
{
ltr.AppendText(arr[i]+","+i+" | ");
}
/*for only last item*/
string lastItem = arraystring[length - 1];
}
结合了许多答案并做了这个,它对我有用。让我知道 Enumerable 方法是否有用。
您可以使用 Row.GetValue<T>()
方法将 Cassandra list<string>
检索到 .NET List<T>
:
var list = row.GetValue<List<string>>("name");
string lastValue = list[list.Count - 1];
我在 Cassandra DB 中有一个列表。 我使用以下代码来获取它们,
string query = "select name from table where userid = 'abcd' ;";
try
{
Connector connector = new Connector();
session = connector.Connect();
RowSet rows = session.Execute(query);
if (rows.Columns.Length > 0)
{
foreach (Row row in rows)
{
data = row["name"].ToString();
}
status = true;
}
}
else
{
// no rows
}
session.Cluster.Shutdown();
}
catch (Exception ex)
{
}
我需要将这个正在转换为字符串的列表对象转换为数组,以便我可以根据需要获取最后一个字符串。
是否有任何内置方法可以做到这一点?
我最后用了下面的代码
foreach (Row row in rows)
{
object a = row["name"];
string[] arr = ((IEnumerable)a).Cast<object>()
.Select(x => x.ToString())
.ToArray();
int ab = arr.Length;
/*for all items*/
for(int i=0; i<ab;i++)
{
ltr.AppendText(arr[i]+","+i+" | ");
}
/*for only last item*/
string lastItem = arraystring[length - 1];
}
结合了许多答案并做了这个,它对我有用。让我知道 Enumerable 方法是否有用。
您可以使用 Row.GetValue<T>()
方法将 Cassandra list<string>
检索到 .NET List<T>
:
var list = row.GetValue<List<string>>("name");
string lastValue = list[list.Count - 1];