【问题标题】:How to compare list values in a dictionary如何比较字典中的列表值
【发布时间】:2016-01-25 04:54:39
【问题描述】:

如何比较Dictionary<String, List<String>>字典中的值?

如果字典中的所有值都相同,那么做一些事情。 否则什么都不做。

谢谢

【问题讨论】:

标签: c# dictionary compare


【解决方案1】:

如果要比较字典的键,那么:

var dict1 = new Dictionary<string, List<string>>();
var dict2 = new Dictionary<string, List<string>>();
// something..
if (dict1.Keys.SequenceEqual(dict2.Keys)) 
{
  // your code
}

如果你想比较字典的值,那么:

var dict1 = new Dictionary<string, List<string>>();
var dict2 = new Dictionary<string, List<string>>();
// something..
var d1Keys = dict1.Keys.ToList();
var result = d1Keys
    .All(key => dict2.ContainsKey(key) && dict1[key].SequenceEqual(dict2[key]));
// result == true, if equals

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多