简体   繁体   English

比较SortedDictionary <string, List<foo> &gt; C#

[英]Compare SortedDictionary<string, List<foo>> C#

I asked similar question here , I am wondering how you can achieve same result with LINQ 我在这里问了类似的问题,我想知道如何使用LINQ达到相同的结果

PREVIOUS QUESTION 上一个问题

Compare Dictionary 比较字典

Rules 规则

Get values from dic2 where key match but values does not match or key is missing in dic2. 从dic2中键匹配但键值不匹配或键不匹配的dic2中获取值。 Don't need to iterate through dic2 for missing/different values in dic1. 对于dic1中缺少/不同的值,不需要遍历dic2。

LINQ CODE LINQ代码

can result and missinkeu can combin strong text e in one? 可以得出结果,而missinkeu可以将强文本 e合并为一吗?

private void Compare(SortedDictionary<string, List<foo>> dic1, SortedDictionary<string, List<foo>> dic2)
 {

            var result=   from c in dic1
                          where dic2.ContainsKey((c.Key) && !dic2[c.key]Equals(c.Value)
                          select p.Value;


            var missingkey =from c in dic1
                          where !dic2.ContainsKey((c.Key) 
                          select p.Value;
 } 

It would be better to write this as a loop instead. 最好将其写为循环。 Here's how I would implement it: 这是我的实现方式:

static void Compare(SortedDictionary<string, List<foo>> dic1, SortedDictionary<string, List<foo>> dic2)
{
    var result = new List<List<foo>>();
    var missingkey = new List<List<foo>>();

    foreach (var kvp in dic1)
    {
        var value = default(List<foo>);
        if (dic2.TryGetValue(kvp.Key, out value))
        {
            if (kvp.Value.SequenceEqual(value))
                result.Add(value);
        }
        else
        {
            missingkey.Add(kvp.Value);
        }
    }
}

If you insist on using LINQ to do this, here's one way to do it: 如果您坚持使用LINQ来执行此操作,则可以采用以下一种方法:

static void Compare(SortedDictionary<string, List<foo>> dic1, SortedDictionary<string, List<foo>> dic2)
{
    const string isIgnored = null, isResult = "result", isMissingkey = "missingkey";
    var combined = (from kvp in dic1
                    group kvp.Value by (dic2.ContainsKey(kvp.Key)
                                            ? kvp.Value.SequenceEqual(dic2[kvp.Key])
                                                  ? isResult
                                                  : isIgnored
                                            : isMissingkey) into g
                    where g.Key != isIgnored
                    select g)
                   .ToDictionary(g => g.Key, g => g.ToList());

    // result in combined["result"]
    // missingkey in combined["missingkey"]
}

Do you just mean: 你是说:

        var result = from pair in dic1
                     where !dic2.ContainsKey(pair.Key)
                        || !dic2[pair.Key].Equals(pair.Value)
                     select pair.Value;

when the key is there the second match is tested. 当钥匙在那儿时,将测试第二场比赛。 Note however, that this will only (as written) do a reference test on the lists - it won't compare individual elements. 但是请注意,这只会 (按书面说明)对列表进行参考测试-不会比较各个元素。 It is hard to tell if that is what you mean since the code example in the linked post is... "unclear" (in that it attempts to add foo as value for List<foo> ). 很难确定这是否是您的意思,因为链接后的代码示例是...“不清楚”(因为它试图将foo添加为List<foo>值)。

If you need to test the individual values: 如果需要测试各个值:

        var result = from pair in dic1
                     where !dic2.ContainsKey(pair.Key)
                     || !dic2[pair.Key].SequenceEqual(pair.Value)
                     select pair.Value;

(which assumes a sensible Equals implementation on foo . (假定对foo了明智的Equals实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM