Comparing two dictionaries for equal data in c#?

前端 未结 5 1518
小蘑菇
小蘑菇 2021-02-07 09:13

I have two dictionaries containing a string key and then an object. The object contains five fields. Is there an elegant way to ensure both dictionaries first contain the same k

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 09:41

    You can use

    bool dictionariesEqual = 
        dic1.Keys.Count == dic2.Keys.Count &&
        dic1.Keys.All(k => dic2.ContainsKey(k) && object.Equals(dic2[k], dic1[k]));
    

提交回复
热议问题