首页 新闻 会员 周边

LINQ查询结果

0
[已解决问题] 解决于 2011-12-16 15:13

大家好,小菜我有个问题请教一下:使用LINQ语句查询SortedDictionary<TKey,TValue>后的结果无法转换成原来的类型,何解?

SortedDictionary<TKey,TValue> dic = new SortedDictionary<TKey,TValue>();

Var temp = from f in dic where f.value.count>0 select f;

TValue是个List<T>集合,我想过滤年为空的记录,但查询结果是个System.Enumerator.Where*******<>类型,无法转换成SortedDictionary<>类型。

一颗空心菜的主页 一颗空心菜 | 初学一级 | 园豆:200
提问于:2011-11-11 11:06
< >
分享
最佳答案
0

其实你没有必要去看temp的类型,它是由编译器自动生成的,并且是IEnumerable<T>的子类,所以我们可以直接使用foreach来遍历它,如下:

            SortedDictionary<string, List<int>> dic = new SortedDictionary<string, List<int>>();
dic.Add("One", new List<int> { 1, 2, 3 });
dic.Add("Two", new List<int> ());
dic.Add("Three", new List<int> { 4, 5, 6 });

var temp = from f in dic where f.Value.Count > 0 select f;

foreach (KeyValuePair<string, List<int>> p in temp)
{
Console.WriteLine("{0} has {1} elements.", p.Key, p.Value.Count);
}


 

Life a Poem | 菜鸟二级 |园豆:437 | 2011-11-11 16:05

这个dic是为了保存一个复杂的数据信息,但要过滤掉list为空的数据,其它很多地方都以这个SortedDictionary做为参数的(这是设计的需要,为了排序却失去了LINQ的支持),所以要转。

我就抛砖引玉了:把这个SortedDictionary<> 换成 Dictionary<TKey,TValue>,然后使TKey实现IComparable<T>接口,解决的LINQ支持和排序问题。不知大家还有什么高招?

一颗空心菜 | 园豆:200 (初学一级) | 2011-11-15 08:58

@一颗空心菜: 

可以像上面的问题那样把代码贴出来不?这样描述不是很清楚。

Life a Poem | 园豆:437 (菜鸟二级) | 2011-11-15 11:12
其他回答(2)
0

为什么要转换为原来的类型?

dudu | 园豆:31003 (高人七级) | 2011-11-11 11:58

我去...谁敢点老大-1~~不想活了??

支持(0) 反对(0) dotNetDR_ | 园豆:2078 (老鸟四级) | 2011-11-12 12:16
0

可能SortedDictionary没有实现IEnumberitor这个接口吧。

悟行 | 园豆:12559 (专家六级) | 2011-11-11 12:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册