Dictionary Union and Sort by value
时间:2010-09-29 来源:IT老民工
1. 把两个DICTIONARY 叠加在一起。并且返回DICTIONARY.
2. 给DICTIONARY的VALUE排序。
我从GOOGLE上找不到现成的, 只好花时间写了两段。希望节约大家的时间。
public static Dictionary<TKey, TValue> UnionDictionary<TKey, TValue> (this Dictionary<TKey, TValue> first, Dictionary<TKey, TValue> second) { Dictionary<TKey, TValue> Rtn = new Dictionary<TKey, TValue>(); var collection = first.Union(second); foreach (var item in collection) { Rtn.Add(item.Key, item.Value); } return Rtn; } public static Dictionary<TKey, TValue> SortByValue<TKey, TValue>(this Dictionary<TKey, TValue> dic) { Dictionary<TKey, TValue> Rtn = new Dictionary<TKey, TValue>(); var collection = dic.OrderBy(item => item.Value); foreach (var item in collection) { Rtn.Add(item.Key, item.Value); } return Rtn; }
相关阅读 更多 +