Get common keys and common meanings of two dictionaries

Hi, I have two dictionaries of the following type:

SortedDictionary<string, ClusterPatternCommonMetadata> PatternMetaData { get; set; }

The ClusterPatternCommonMetadata object looks like this:

int ChunkQuantity { get; set; }

SortedDictionary<int, int> ChunkOccurrences { get; set; }

First I need a way to find PatternMetaData keys that exist in two dictionaries. I find this way:

List<string> commonKeysString=
            vector.PatternMetaData.Keys.Intersect(currentFindingVector.PatternMetaData.Keys)

Then I need to find the common values ​​of the keys based ...

Is there a quick way (lambda, linq, etc.) to perform this operation

thank

+5
source share
2 answers

This is called an intersection.

You can get the keys using

var data = dictionary1.Keys.Intersect(dictionary2.Keys)

If you want to find equal keys and values ​​that are in both dictionaries, just

var equalDictionarys = dictionary1.Intersect(dictionary2);
+9
source

You can also get entire dictionary entries that have common keys:

var commonDictionaryItems = Dic1.Where(d => Dic2.ContainsKey(d.Key)).ToList();
+1
source

All Articles