How do I insertsect two lists of KeyValuePairs based on their keys? I have tried:
List<KeyValuePair<string, string>> listA = new List<KeyValuePair<string, string>>();
List<KeyValuePair<string, string>> listB = new List<KeyValuePair<string, string>>();
...
var result = listA.Intersect(listB);
Which expectedly doesn’t work. Do I need to write my own comparer based on the key or is there a easy way using LINQ/Lambda?
Thanks!
Note that this code mimics the behaviour of
Intersectby using theRemovemethod. This means that both sequences are treated as sets: if there are multiple items with the same key inlistAthenresultwill only contain one of those items. If you don’t want this behaviour then use theContainsmethod instead ofRemove.