Linq: Except on two different types of dictionaries

17,058

Solution 1

It would be easier to just create a new dictionary based on the new dictionary ignoring items that have the same key in the old dictionary.

var result = newDic
    .Where(kvp => !oldDic.ContainsKey(kvp.Key))
    .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

Solution 2

Note: Despite your question saying "i need to compare values in both dictionaries" (emphasis mine) your example seems to be demonstrating just comparison of keys so I've gone with that. If it is values you need to compare you might want to give an example of what you mean and if they are easily convertible or comparable...

If you are only actually comparing the keys then you can just use the .Keys property of the dictionary which returns an IEnumerable<TKey> that you can do your linq on...

eg:

var expectedOutput = newDic.Keys.Except(oldDic.Keys);

This does rely on the key being the same type but this goes without saying if you are comparing. Of course, there is nothing stopping you from converting their types first if you do wnat to do this with different types.

Also if you wanted to then get the values in one of the dictionaries you could do something like:

var newDicValues = expectedoutput.Select(x=>newDic[x]);

Or, you know, do any other linqy stuff you feel like. :)

Solution 3

Try this to get the difference between two different list: If they have any common property.

 var differentItems = List<Type1>.Select(d => d.Name)
                        .Except(List<Type2>.Select(d => d.Name));
Share:
17,058
Mayank Pathak
Author by

Mayank Pathak

I am an Application and Information Security enthusiast. I do consultation for Security Operations, Application Security Architecture, Penetration testing, helping development teams to integrate security into CI/CD process. I work with both Java and .Net based enterprise applications and help them architecture in secure way. I have good experience with working on highly transactional systems. For any project/consultation contact me at [email protected]

Updated on June 04, 2022

Comments

  • Mayank Pathak
    Mayank Pathak almost 2 years

    i have 2 different types of dictionaries Dictionary<int, Fooclass> oldDic and Dictionary<int, string> barList newDic . Now i need to compare values in both dictionaries. for Example key can be in

    • oldDic : 1,2,3
    • newDic : 1,2,3,4
    • Expected Output : 4

    Now i need to compare both dictionaries on basis of their keys any help would be appreciated.

    Edit : Output should be like second dictionary(newDic) but this will contain some value of 2nd dictionary's(oldDic). For example

    1,"fooString" Where fooString is some value in Fooclass's someproperty.... For more clarity see this which doesn't worked for me

    var addedList = from list1 in baseListDic
    join list2 in trackerlist on list1.Key equals list2.Key
     select new { key = list1.Key, value = list1.Value._lead };
    

    here baseListDic is oldDic and trackerlist is newDic.... Let me know if i'm still not clear...

  • Chris
    Chris almost 12 years
    since all the x came from oldDic you shouldn't need to use TryGetvalue... In fact I'd have thought that this would be simpler as OldDic.Where(x=> !NewDic.ContainsKey(x.Key)).ToDictionary(x=>x.Key, x=>x.Value)