2

I want to compare two dictionaries in c# so as to get the output as 'True' and 'False'.Here is the code i have so far :

var dict3 = Dict.Where(entry => Dict_BM[entry.Key] != entry.Value)
                .ToDictionary(entry => entry.Key, entry => entry.Value);

I have two different dictionaries names as 'Dict' and 'Dict_BM' and i want to compare these two dictionaries.Please suggest the best possible way to get the output as 'True' and 'False' Thanks!

  • Please check this post http://stackoverflow.com/questions/5411737/best-way-to-compare-two-dictionaryt-for-equality – Pankaj Dey Aug 14 '13 at 04:09

2 Answers2

4

i think this is quicekest way to compare two dictionaries and return those results are true or false.

This logic has been worked for me.

                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");
                }

Hope this helpful for you.

Mister X
  • 3,406
  • 3
  • 31
  • 72
1

If you want to construct a new dictionary where the value at each key is a Boolean value indicating if the entries in the two source dictionaries are equal try this:

var dict3 = Dict.ToDictionary(
    entry => entry.Key, 
    entry => Dict_BM[entry.Key] == entry.Value);

If it's possible that the two source dictionaries do not contain the same keys, you might want to try something like this:

var dict3 = Dict.Keys.Union(Dict_BM.Keys).ToDictionary(
    key => key, 
    key => Dict.ContainsKey(key) && 
           Dict_BM.ContainsKey(key) && 
           Dict[key] == Dict_BM[key]);

However, if all you'd like to do is test to see if the two dictionaries contain exactly the same elements, you can simply use this:

var areEqual = Dict.SequenceEqual(Dict_BM);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Is there any other way for comparison without creating a new dictionary for comparison part. – user2364821 Aug 14 '13 at 03:50
  • @user2364821 If you are just trying to test to see if the two dictionaries contain precisely the same values, please see my update. – p.s.w.g Aug 14 '13 at 03:53
  • +1, `SequenceEqual` works on `Dictionary` since `KeyValuePair` is a `struct` – cuongle Aug 14 '13 at 03:57
  • @CuongLe Thanks. Just to be clear, it would work on any type that overrides the `Equals` method, not just `structs`. – p.s.w.g Aug 14 '13 at 04:00