比较两个字典并返回另一个字典中的差异 [英] Comparing two dictionaries and returning the differences in another dictionary

查看:50
本文介绍了比较两个字典并返回另一个字典中的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字典,我想比较两个字典的键/对值.当我比较它们时,如果第二个字典中的值不同,我想保持它们的配对并将其存储到字典 3 中.

I have two dictionaries and I wish to compare the two dictionaries key / pair values. When I compare them if the value is different in the second dictionary I want to keep their pairing and store that into dictionary3.

所以如果我有字典 1 和 (<1,T><2,T><3,T>) 和 2 (<1,T>)<2,F><3,T>) 我希望 3 看起来像 (<2,F>).

So if I have dictionary 1 with (<1,T><2,T><3,T>) and 2 with (<1,T><2,F><3,T>) I want 3 to look like (<2,F>).

我不知道从哪里开始这个.我有字典正确获取所有数据,但现在我不确定如何设置比较.

I am not sure where to start with this one. I have the dictionaries properly getting all their data but right now I am not sure how to set up the compare.

private Dictionary<int, bool> CompareDictionaries(Dictionary<int, bool> dic2)
{
    Dictionary<int,bool> dictionary3 = new Dictionary<int,bool>();

    foreach (KeyValuePair<int, bool> pair in dictionary1)
    {
        // keep KeyValuePair of dic2
        // dictionary3.add(KeyValuePair of dic 2)
    }

    return dictionary3;
}

对此的任何帮助将不胜感激.我很肯定我可以用字典实现我的目标.在我获得第三本字典后,我将更新表格中的一些信息,然后刷新我正在显示的列表,但该部分要容易得多,然后找出我需要的方法和算法.任何帮助一如既往地非常感谢.谢谢各位.

Any help on this would be appreciated. I am pretty positive that I can accomplish my goal with dictionaries. Down the line after I get the 3rd dictionary I am going to update some information in a table and then refresh a list I am displaying but that part is much easier then figure out what methods and algorithm I need for this part. Any help is as always very very appreciated. Thank guys.

推荐答案

var dict3 = dict2.Except(dict1).ToDictionary(x => x.Key, x => x.Value);

这篇关于比较两个字典并返回另一个字典中的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆