简体   繁体   English

比较2清单 <Dictionary<string,object> &gt;在C#中

[英]Compare 2 List<Dictionary<string,object>> in c#

I have 2 list of dictionaries 我有2本字典清单

List<Dictionary<string,object>> master
List<Dictionary<string,object>> sub

Both my dictionaries have identical Key names. 我的两个词典都有相同的键名。

EX: The first list Master contains
Master.Add(new Dictionary<string, string>(){

{"key1","SAME1"}
{"key2", "value1"},
{"key3","value2"}
});
Master.Add(new Dictionary<string, string>(){
{"key1","SAME2"}
{"key2", "value3"},
{"key3","value5"}
});

Master.Add(new Dictionary<string, string>(){
{"key1","SAME77"}
{"key2", "value55"},
{"key3","value44"}
});

The second list sub contains similar kind of key,value pairing :

sub.Add(new Dictionary<string, string>(){
{"key1","SAME1"}
{"key2", "value7"},
{"key3","value9"}
});

Master.Add(new Dictionary<string, string>(){
{"key1","SAME2"}
{"key2", "value3"},
{"key3","value4"}
});

Is there a way in LinQ(or any other easier way) i can use to compare the 2 list of dictionaries and get an output Dictionary. 我可以使用LinQ中的一种方法(或任何其他更简便的方法)来比较字典的2个列表并获得输出Dictionary。 Example

Scenario 1: As you can see i can use one of the Keys(Key1 since it is the same Value for all) from the first list of dictionaries to see if that Value exists in the second dictionary and if it does not i want the missing dictionary in a new List> . 方案1:如您所见,我可以使用第一个字典列表中的一个键(Key1,因为它的所有值都相同),以查看该值是否存在于第二个词典中,如果不存在,我希望缺少它新列表中的字典>。

Output Ex: NewList.Add(new Dictionary<string, string>(){
{"key1","SAME77"} // since SAME77 isn't present in sub dictionary 
{"key2", "value55"},
{"key3","value44"}
}

Scenario 2: If suppose one of the dictionary in the sub list of dictionaries contains the same value for the key1 , I want to check the other values of the same dictionary and if any values for those keys have changed and if they have i want a new List having the original one. 方案2:如果假设词典子列表中的一个词典包含key1的相同值,则我想检查同一词典的其他值,以及这些键的任何值是否已更改,如果它们具有,我想要一个具有原始列表的新列表。

ex output : ex输出:

NewList.Add(new Dictionary<string, string>(){
{"key1","SAME2"}
{"key2", "value3"},
{"key3","value5"} // since the master dictionary has value5 and the sublist dictionary has value 4 

} }

Scenario 1 Attempt: 方案1尝试:

var result = Master.SelectMany(m=>m).Where(e=>sub.SelectMany(a=>a)
                .Any(p => e.Key == p.Key && p.Value!=null && e.Value!=(p.Value)));

However this does not return the correct result and the output is in IEnumerable. 但是,这不会返回正确的结果,并且输出为IEnumerable。 Is there away i can get the required output through linq itself? 我可以通过linq本身获得所需的输出吗?

Appreciate any help 感谢任何帮助

As a caveat, your data structure is hardly ideal. 请注意,您的数据结构并不理想。 Since you are treating the first value of the first key as your "key", you might want to consider recreating your structure to use your "SAME1" (and similar values) as the keys. 由于您将第一个键的第一个值视为“键”,因此您可能需要考虑重新创建结构,以将“ SAME1”(和类似值)用作键。

That said, you can still accomplish both queries in "single-line" LINQ statements. 也就是说,您仍然可以在“单行” LINQ语句中完成两个查询。

var masterOnly = Master.Where(m => 
    !sub.Select(s => s.First().Value).Contains(m.First().Value)
).ToList();

In the master only scenario, we use a Where clause to allow us to first build out a collection of sub -> first -> values and we then check each master -> first -> value in our loop against that collection. 在仅主机的情况下,我们使用Where子句允许我们首先构建子-> first->值的集合,然后针对该集合检查循环中的每个master-> first->值。 If the value does not exist, we return the value. 如果该值不存在,则返回该值。

var hasChanged = Master.Where(m => 
    sub.FirstOrDefault(s => 
        s.First().Value == m.First().Value)?.SequenceEqual(m) == false)
    .ToList();

In this scenario, we can take advantage of C#6's null propagation operator to avoid needing to use .ForEach() to build a match in a loop since if the value is null, our SequenceEquals check is ignored. 在这种情况下,我们可以利用C#6的null传播运算符来避免使用.ForEach()在循环中构建匹配项,因为如果该值为null,则将忽略SequenceEquals检查。

In the case where we have a match, we want to explicitly compare its sequence against our master collection and if any entry does not match, we return it. 如果我们有一个匹配项,我们想将其序列与我们的主集合进行显式比较,如果任何条目不匹配,我们将其返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM