作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想比较 C# 中的两个字典,以便将输出设为“True”和“False”。这是我目前的代码:
var dict3 = Dict.Where(entry => Dict_BM[entry.Key] != entry.Value)
.ToDictionary(entry => entry.Key, entry => entry.Value);
我有两个不同的词典名称,分别是“Dict”和“Dict_BM”,我想比较这两个词典。请建议获得输出“True”和“False”的最佳方法谢谢!
最佳答案
我认为这是比较两个字典并返回结果是真还是假的最快捷方式。
这个逻辑对我有用。
Dictionary<string, int> Dictionary1 = new Dictionary<string, int>();
d1.Add("data1",10);
d1.Add("data2",11);
d1.Add("data3",12);
Dictionary<string, int> Dictionary2 = new Dictionary<string, int>();
d2.Add("data3", 12);
d2.Add("data1",10);
d2.Add("data2",11);
bool equal = false;
if (Dictionary1.Count == Dictionary2.Count) // Require equal count.
{
equal = true;
foreach (var pair in Dictionary1)
{
int tempValue;
if (Dictionary2.TryGetValue(pair.Key, out tempValue))
{
// Require value be equal.
if (tempValue != pair.Value)
{
equal = false;
break;
}
}
else
{
// Require key be present.
equal = false;
break;
}
}
}
if (equal == true)
{
Console.WriteLine("Content Matched");
}
else
{
Console.WriteLine("Content Doesn't Matched");
}
希望对您有所帮助。
关于c# - 如何在 C# 中比较两个字典并在验证后得到输出为 True 和 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222855/
我是一名优秀的程序员,十分优秀!