gpt4 book ai didi

c# - 如何在 C# 中比较两个字典并在验证后得到输出为 True 和 False

转载 作者:行者123 更新时间:2023-12-02 04:54:37 24 4
gpt4 key购买 nike

我想比较 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com