无法使用 DataStax C# Drive 将 cassandra 中的 "Set" 转换为 c# 中的 "SortedSet" 或 "HashSet"
Unable to cast "Set" in cassandra to "SortedSet" or "HashSet" in c# using DataStax C# Drive
Cassandra
table 列如下所示:
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
函数 returns 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);
当我尝试将类型为 set<int>
的 setint
转换为 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].
考虑到我的 setint
是 set<int>
而不是 list<int>
,为什么我会得到这个 execption?
您应该使用通用 Row.GetValue<T>(columnName)
方法:
SortedSet<int> setValue = row.GetValue<SortedSet<int>>("setint");
List<int> listValue = row.GetValue<List<int>>("listint");
Cassandra
table 列如下所示:
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
函数 returns 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);
当我尝试将类型为 set<int>
的 setint
转换为 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].
考虑到我的 setint
是 set<int>
而不是 list<int>
,为什么我会得到这个 execption?
您应该使用通用 Row.GetValue<T>(columnName)
方法:
SortedSet<int> setValue = row.GetValue<SortedSet<int>>("setint");
List<int> listValue = row.GetValue<List<int>>("listint");