开发者

Correct way to compare two List<KeyValuePair<string, Object>> in C# 2.0

开发者 https://www.devze.com 2023-01-21 18:14 出处:网络
Is this correct way to compare in C# 2.0 (NO LINQ ). Below code is working ok but I think its not good way to compare.

Is this correct way to compare in C# 2.0 (NO LINQ ).

Below code is working ok but I think its not good way to compare.

        List<KeyValuePair<string, foo>> list1 = new List<KeyValuePair<string, foo>>();
        List<KeyValuePair<string, foo>> list2 = new List<KeyValuePair<string, foo>>();
        List<foo> diffList = new List<foo>();

        list1.Add(new KeyValuePair<string, foo>("1", new foo("1", new Cost(1.0)));
        list1.Add(new KeyValuePair<string, foo>("2", new foo("2", new Cost(2.0)));
        list1.Add(new KeyValuePair<string, foo>("3", new foo("3", new Cost(3.0)));
        list1.Add(new KeyValuePair<string, foo>("5", new foo("5", new Cost(5.0)));

        list2.Add(new KeyValuePair<string, foo>("1", new foo("1", new Cost(1.0));
        list2.Add(new KeyValuePair<string, foo>("2", new foo("2", new Cost(2.1)));
        list2.Add(new KeyValuePair<string, foo>("4", new foo("4", new Cost(4.0));
        list2.Add(new KeyValuePair<string, foo>("6", new foo("6", new Cost(6.0)));
        list2.Add(new KeyValuePair<string, foo>("7", new foo("7", new Cost(7.0)));

        foreach (KeyValuePair<string, foo> pair1 in list1)
        {
            bool b = true;
            foreach (KeyValuePair<string, foo> pair2 in list2)
            {
                if (pair2.Key == pair1.Key)
                {
                    if (pair2.Value.Equals(pair1.Value))
                    {
                        list2.Remove(pair2);
                        break;
                    }
                    else
                    {
                        diffList.Add(pair2.Value);
                        diffList.Add(pair1.Value);
                        list2.Remove(pair2);
                        b = false;
                        break;
                    }
                }
      开发者_运维问答          else
                {

                    diffList.Add(pair2.Value);
                    diffList.Add(pair1.Value);
                    list2.Remove(pair2);
                    b = false;
                    break;
                }
            }
            if (list2.Count == 0 && b)
            {
                diffList.Add(pair1.Value);
            }
        }
        foreach (KeyValuePair<string, foo> pair2 in list2)
        {
            diffList.Add(pair2.Value);
        }


It would be simpler and faster to:

  1. Push both lists into dictionaries (or build dictionaries in the first place).
  2. Iterate over one dictionary, looking up each key in the other dictionary, adding diff entries accordingly. Only add entries from the dictionary you are iterating over.
  3. Swap the dictionaries and repeat the loop.


Marcelo is right, a list of KeyValuePairs is always better represented as a dictionary UNLESS you want to be able to have duplicate keys for some reason.

Try something like this:

var a = list1.ToDictionary(i => i.Key, i => i.Value);
var b = list2.ToDictionary(i => i.Key, i => i.Value);
var result = new List<foo>();

foreach (var entry in a)
{
    if (b.ContainsKey(entry.Key) 
        && entry.Value != b[entry.Key])
    {
        result.Add(entry.Value);
        result.Add(b[entry.Key]);
    }
}
foreach (var entry in b)
{
    if (a.ContainsKey(entry.Key) 
        && entry.Value != a[entry.Key] 
        && !result.Contains(entry.Value))
    {
        result.Add(entry.Value);
        result.Add(a[entry.Key]);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消