比较两个键值字典C#

时间:2017-04-20 21:19:53

标签: c# dictionary

我有两本词典

1具有字典A(期望值)和字典B.

它们都是字典,我希望能够说,如果B.Keys包含A中的键,则比较这两个值并返回true或false。

我怎么写这样的东西:

$ links -dump "http://www.ned.org/region/central-and-eastern-europe/belarus-2016/" |egrep -o '\$[0-9]+([.,][0-9]+)*'
$43,270
$25,845
$55,582
$14,940
$44,100
$35,610
$54,470
$60,200
$33,150
$15,720
$35,160
$45,500
$72,220
$26,330
$53,020
$27,710
$22,570
$40,145
#more prices following bellow

键值示例:

if (B.ContainsKey(A.Keys))
        {

        }

1 个答案:

答案 0 :(得分:3)

扫描一个字典并在第二个字段中查找并进行比较,如下所示:

var a = new Dictionary<TK,TV>();
var b = new Dictionary<TK,TV>();

var isEquals = a.All(x =>
{
    TV v;
    if (b.TryGetValue(x.Key, out v))
       return x.Value.Equals(v);

    return false;
});

<强> UPD

isEquals = true;
foreach(x in a)
{
    TV v;
    if (b.TryGetValue(x.Key, out v))
       isEquals = isEquals && x.Value.Equals(v);
    else
    {
       isEquals = false;
       break;
    }

}