How to compare two Dictionaries in C#

后端 未结 11 1278
南方客
南方客 2020-12-05 10:03

I have two Generic Dictionaries.Both have same keys.But values can be different.I want to compare 2nd dictionary with 1st dictionary .If there are differences between values

11条回答
  •  时光取名叫无心
    2020-12-05 10:46

    Assuming both dictionaries have the same keys, the simplest way is

    var result = a.Except(b).ToDictionary(x => x.Key, x => x.Value);
    

    EDIT

    Note that a.Except(b) gives a different result from b.Except(a):

    a.Except(b): Price     20
    b.Except(a): Price     40
    

提交回复
热议问题