NitroFrost NitroFrost - 20 days ago 9
C# Question

Comparing two dictionaries and returning the differences in another dictionary

I have two dictionaries and I wish to compare the two dictionaries key / pair values. When I compare them if the value is different in the second dictionary I want to keep their pairing and store that into dictionary3.

So if I have dictionary 1 with (

<1,T><2,T><3,T>
) and 2 with (
<1,T><2,F><3,T>
) I want 3 to look like (
<2,F>
).

I am not sure where to start with this one. I have the dictionaries properly getting all their data but right now I am not sure how to set up the compare.

private Dictionary<int, bool> CompareDictionaries(Dictionary<int, bool> dic2)
{
Dictionary<int,bool> dictionary3 = new Dictionary<int,bool>();

foreach (KeyValuePair<int, bool> pair in dictionary1)
{
// keep KeyValuePair of dic2
// dictionary3.add(KeyValuePair of dic 2)
}

return dictionary3;
}


Any help on this would be appreciated. I am pretty positive that I can accomplish my goal with dictionaries. Down the line after I get the 3rd dictionary I am going to update some information in a table and then refresh a list I am displaying but that part is much easier then figure out what methods and algorithm I need for this part. Any help is as always very very appreciated. Thank guys.

I4V I4V
Answer
var dict3 = dict2.Except(dict1).ToDictionary(x => x.Key, x => x.Value);
Comments