using System; using System.Collections.Generic; using System.Linq; using System.Text; //namespace Utils.Datas namespace System.Collections.Generic { public static class DictionaryUtil { /// /// 将第二个参数中的数据合并到第一个参数中。【通过Key判断。如果已存在,则忽略】 /// Key不能为类 /// public static void Combine(this IDictionary a, IDictionary b) { if (a == null) a = new Dictionary(); if (b == null || b.Count == 0) return; foreach (var item in b) { if (!a.Keys.Contains(item.Key)) a.Add(item.Key, item.Value); } } /// /// 找出两个参数中,Key相同的项的Key /// Key不能为类 /// public static List FindSameKey(this IDictionary a, IDictionary b) { List result = new List(); foreach (var item in a) { if (b.Keys.Contains(item.Key)) result.Add(item.Key); } return result; } /// /// 从第一个参数中去掉第二个参数中的所有项后,剩余项的Key。只比较Key /// Key不能为类 /// public static List SubKey(this IDictionary a, IDictionary b) { List result = new List(); foreach (var item in a) { if (!b.Keys.Contains(item.Key)) result.Add(item.Key); } return result; } public static Dictionary ChangeValueToString(this IDictionary dic) {//TODO:添加测试 Dictionary result = new Dictionary(); foreach (var detail in dic) { result.Add(detail.Key, detail.Value == null ? string.Empty : detail.Value.ToString()); } return result; } } }