2012-05-14 67 views
5

您好我有未來型的兩點字典:獲得兩個庫的公共密鑰和共同的價值觀

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

的ClusterPatternCommonMetadata物體看起來像:

int ChunkQuantity { get; set; } 

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

首先我需要找到的按鍵方式PatternMetaData存在於兩個字典中。我覺得是這樣的:爲了做這樣的操作

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

然後我需要找到創立鍵的共同價值觀......

是否有快速的方法(拉姆達,LINQ等等)

謝謝

+0

您是否在尋找匹配鍵或匹配鍵/值?相關問題:http://stackoverflow.com/questions/3804367/testing-for-equality-between-dictionaries-in-c-sharp – deepee1

回答

9

這就是所謂的十字路口。

可以使用

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

拿到鑰匙了。如果你想找到包含兩個字典中相等的鍵和值,然後只需

var equalDictionarys = dictionary1.Intersect(dictionary2); 
+0

因此,沒有辦法通過兩個給定字典的鍵和值來獲得相交字典? – AlexBerd

+0

@AlexanderBerdichevsky只是爲了澄清..你想獲得所有的鍵盤和價值匹配的詞典? – AlanFoster

+0

我想從兩個裏面創建一個字典,它將共同的鍵和鍵的共同價值 – AlexBerd

1

您還可以得到整個字典項其中有共同的鍵:

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