2
在C#中,如何使用LINQ過濾SortedDictionary生成也是SortedDictionary的子集?例如。我想寫將SortedDictionary的子集作爲SortedDictionary使用
SortedDictionary<int, Person> source = ..fetch..
SortedDictionary<int, Person> filtered = source.Where(x=>x.foo == bar)
我發現的唯一的方法是創建一個輔助方法和使用
SortedDictionary<TKey, TValue> SubDictionary<TKey, TValue> IEnumerable<KeyValuePair<TKey, TValue>> l)
{
SortedDictionary<TKey, TValue> result = new SortedDictionary<TKey, TValue>();
foreach (var e in l)
result[e.Key] = e.Value;
return result;
}
...
SortedDictionary<int, Person> source = ..fetch..
SortedDictionary<int, Person> filtered = SubDictionary(source.Where(x=>x.foo == bar))