如何比较两个词典,我的词典如下
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("One", "One");
dic.Add("Two", "Two");
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic1 .Add("One", "One");
dic1 .Add("Two", "Two");
dic1 .Add("Three", "Three");
我尝试了一些像var diff = dic1.Where(x => x.Value != dic[x.Key]).ToDictionary(x => x.Key, x => x.Value);
这样的东西,但我得到了异常,因为密钥不匹配所以有人可以帮助我
答案 0 :(得分:3)
这会给你带来不同,
var resultDic = dic1.Except(dic).ToDictionary(x => x.Key, x => x.Value);
答案 1 :(得分:1)
private void Comparer(Dictionary<string, string> dic, Dictionary<string, string> dic1)
{
var diff = dic.Except(dic1).Concat(dic1.Except(dic));
}