Home > OS >  Comparing Dictionaries and finding new objects
Comparing Dictionaries and finding new objects

Time:09-07

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?

CodePudding user response:

using linq you can do

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

CodePudding user response:

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