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

This is the correct way to compare in C # 2.0 (NO LINQ).

Below code works fine, but I think its a 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);
        }
+3
source share
2 answers

It would be simpler and faster:

  • Insert both lists into dictionaries (or create dictionaries first).
  • Go through one dictionary by looking at each key in another dictionary, adding diff entries accordingly. Only add words from the dictionary you iterate.
  • Change the dictionaries and repeat the cycle.
+2
source

Marcelo is right, the KeyValuePairs list is always better represented as a dictionary IF you don’t want to have duplicate keys for some reason.

- :

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]);
    }
}
+2

Source: https://habr.com/ru/post/1771385/


All Articles