【发布时间】:2013-05-02 03:34:55
【问题描述】:
我有一个Dictionary<string,int>,其中包含一些值。例如:
- “一”,600
- "b", 550
- “c”,400
我想过滤此列表以保留大于 500 的值。我不经常使用 C#,也没有使用任何 LINQ。我认为这可能是一个学习的好时机。所以,我尝试了以下方法:
Dictionary<string,int> someDictionary = new Dictionary();
// Code to populate someDictionary goes here
someDictionary = (Dictionary<string,int>) someDictionary.Where(pair => pair.Value > 500);
这会引发 InvalidCastException:
无法将“WhereEnumerableIterator`1[System.Collections.Generic.KeyValuePair`2[System.String,System.Int32]]”类型的对象转换为“System.Collections.Generic.Dictionary`2[System.String]”类型的对象,System.Int32]'。
我尝试了一些强制转换,调用.ToDictionary() 等。我似乎无法弄清楚如何正确地强制转换,或者让语法完全正确。你能为我指出正确的方向吗?感谢您的宝贵时间。
【问题讨论】: