2012-05-14 46 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();