I’ve two dictionaries as following:
Dictionary 1:
Dictionary<string, string> dict1 = new Dictionary<string, string>();
request.Add("key1", "value1");
request.Add("key2", "value2");
request.Add("key3", "value3");
Dictionary 2 :
Dictionary<string, string> request = new Dictionary<string, string>();
request.Add("key1", "value1");
request.Add("key2", "value2");
I need to compare above two dictionaries using LINQ query with condition:
1) All keys in dict2 should match with keys in dict1
2) The matched keys should have equivalent value
3) Even if the value of key2 in dict2 is empty, it should match
Help on above in appreciated. Thanks in advance.
Regards,
Sachin
You could use the
Containsmethod and provide a customIEqualityComparer, but an easier way would be to useAny():I guess with empty you mean
nullor an empty string, hence I usedString.IsNullOrEmpty. If you want to just check fornull, do a simplek2.Value == nullinstead.