C# Compare two dictionaries for equality

后端 未结 10 1113
独厮守ぢ
独厮守ぢ 2020-12-01 10:41

I want to compare in C# two dictionaries with as keys a string and as value a list of ints. I assume two dictionaries to be equal when they both ha

10条回答
  •  日久生厌
    2020-12-01 11:31

    Convert the dictionary to a KeyValuePair list and then compare as collections:

    CollectionAssert.AreEqual(
       dict1.OrderBy(kv => kv.Key).ToList(),
       dict2.OrderBy(kv => kv.Key).ToList()
    );
    

提交回复
热议问题