我想比較兩個字典,該程序是用C#Visual Studio 2010中鍵進行比較的兩個字典
Dictionary<int, string> members1 = new Dictionaries<int, string>{
{1, "adam"},
{2, "bob"},
{3, "cameron"}
}
Dictionary<int, string> members2 = new Dictionaries<int, string>{
{1, "adam"},
{2, "bill"},
{4, "dave"}
}
我想找到相同的ID(鍵),它不」無論名字(價值)是否相同,都無關緊要。
我一直在尋找,發現Intersect
和Except
,但我不認爲它的工作方式我想要它。
以上例爲例,如果我調用Intersect
函數,我希望它返回List<int>{1, 2}
。
如果我把類似members1.Except(members2)
,我想它返回
Dictionary<int, string> intersectMembers{
{1, "adam"},
}
一個解決方案,我認爲做的是寫2 for循環,並使用dictionary.Contains(key)
得到我想要的結果。
有沒有更直接的方法來做到這一點?
感謝
我以爲他想要第三本字典嗎? –
@JasonRoell但OP說*我想要它返回列表 {1,2} * –
哦,我想我只是看到部分關於「如果我打電話像members1.Except(members2),我想它返回 詞典 intersectMembers {0,1,「adam」}, }「 –