gpt4 book ai didi

c# - 无法使用DataStax C# Drive将cassandra中的 "Set"转换为c#中的 "SortedSet"或 "HashSet"

转载 作者:行者123 更新时间:2023-11-30 16:51:34 26 4
gpt4 key购买 nike

Cassandra表格列如下:

CREATE TABLE mine.testtable (
id int,
listtext text,
myid int,
name text,
name1 int,
setint set<int>,
stringtext text,
testlist list<int>,
testmap map<int, text>,
testset set<text>,
textlist list<text>,
PRIMARY KEY (id)
) WITH read_repair_chance = 0.0
AND dclocal_read_repair_chance = 0.1
AND gc_grace_seconds = 864000
AND bloom_filter_fp_chance = 0.01
AND caching = { 'keys' : 'ALL', 'rows_per_partition' : 'NONE' }
AND comment = ''
AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy' }
AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.LZ4Compressor' }
AND default_time_to_live = 0
AND speculative_retry = '99.0PERCENTILE'
AND min_index_interval = 128
AND max_index_interval = 2048;

在 C# 中,getColumnValue函数返回 Cassandra 的一列排。该列可以有任何 Type .我的功能是这样的:

public Object getColumnValue(string tableName, string columnName, int id)
{
string tmp = string.Format("SELECT {0} FROM {1} WHERE id={2};", columnName ,tableName, id.ToString());
Row row = Session.Execute(tmp).GetRows().First();
return row.GetValue(row.GetType(), columnName);
}

可以像这样为不同的列名调用函数:

 SortedDictionary<int, string> dictionary =(SortedDictionary<int, string>)getColumnValue("testtable", "testmap", 3);
List<string> text =(List<string>)getColumnValue("testtable", "textlist", 2);
SortedSet<int> setext = (SortedSet<int>)getColumnValue("testtable", "setint", 2);

当我尝试施放 setint 时类型为 set<int>SortedSet<int>HashSet<int>我收到如下错误:

An unhandled exception of type 'System.InvalidCastException' occurred in Service.exe

Additional information: Unable to cast object of type System.Collections.Generic.List1[System.Int32] to type System.Collections.Generic.SortedSet`1[System.Int32].

考虑到我的 setintset<int>而不是 list<int> , 为什么我会得到这个异常?

最佳答案

您应该使用通用的 Row.GetValue<T>(columnName)方法:

SortedSet<int> setValue = row.GetValue<SortedSet<int>>("setint");
List<int> listValue = row.GetValue<List<int>>("listint");

关于c# - 无法使用DataStax C# Drive将cassandra中的 "Set"转换为c#中的 "SortedSet"或 "HashSet",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33730950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com