NitroFrost NitroFrost - 1 year ago 67
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 (

) and 2 with (
) I want 3 to look like (

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.

Answer Source
var dict3 = dict2.Except(dict1).ToDictionary(x => x.Key, x => x.Value);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download