【发布时间】:2018-09-24 23:38:39
【问题描述】:
我有一个列表,其中包含如下数据:
megalist = { new List {1,2}, new List {1,2}, new List{3}};
现在,我想将这个 IList 列表转换为一个扁平化的 hashset,应该如下所示:
set = { 1,2,3 }
我试着做
megalist.Cast<ISet<int>>().SelectMany(sublist => sublist); 但返回错误:
无法将“System.Collections.Generic.List'1[System.Int32]”类型的对象转换为“System.Collections.Generic.ISet'1[System.Int32]”类型。
这种方法有问题吗? 非常感谢。
【问题讨论】:
-
Is something wrong with the approach?是的。您不能将List<T>(或IList<T>)转换为ISet<T>,因为这两件事(或代表的事物)在本质上彼此完全不同...... -
建议:先将
IList<IList<T>>展平,然后将结果输入新的HashSet<T>。
标签: c# list casting ienumerable