Comparing Dictionaries and finding new objects

Viewed 41

I need to compare two dictionaries x and y. How to find that dictionary y is having some new elements which are not available in dictionary x?

2 Answers

using linq you can do

bool yContainsNew = y.Any(kv => !(x.ContainsKey(kv.Key) && x[kv.Key] == kv.Value));

Using LINQ

        Dictionary<int, object> x = new()
        {
            {0,null },
            {1,null },
            {7,null },
            {4,null }
        };

        Dictionary<int, object> y = new(x); // new y with x values
        y.Add(3, null);
        foreach(var e in y.Except(x)){
            Console.WriteLine(e);
Related