Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking all entries from one Dictionary are in another Dictionary

Tags:

c#

compare

i have two Dictionarys A & B, i want to see if all entries in A exist in B. In the past i've compared Lists using the following:

var set1 = new HashSet<String>(list1);
var set2 = new HashSet<String>(list2);

return set1.SetEquals(set2);

What i have thought to do is simply loop over each value in Dictionary A using:

dictA.TryGetValue(dictBvalue, out item)

this will return null on the item var if the value isn't there, but this seems a little long winded.

Is there a quick and effcient way of comparing dictionaries?

Thanks.

like image 680
Dan Hall Avatar asked Sep 02 '25 17:09

Dan Hall


1 Answers

You could use All extension and do this.

var allexist = list1.All(x=> list2.ContainsKey(x.Key) && list2[x.Key] == x.Value)
like image 60
Hari Prasad Avatar answered Sep 04 '25 06:09

Hari Prasad