我对 Linq / Lambda 和以下问题有疑问:
我有两个字典,primary 和secondary...这两个字典定义为Key=string,Value=int。如果 KEYS 与辅助字典相交,我需要修剪主字典。
IE:
primaryDict = ["thing1", 33] ["thing2", 24] ["thing3", 21] ["thing4", 17] ["thing5", 12]
secondaryDict = ["thing1", 22] ["thing3", 20] ["thing4", 19] ["thing7", 17] ["thing9", 10]
resultDict = ["thing1", 33] ["thing3", 21] ["thing4", 17]
我的尝试:
resultDict = primaryDict.Keys.Intersect(secondaryDict.Keys).ToDictionary(t => t.Key, t.Value);
这显然不起作用,因为 primaryDict.Keys.Intersect 正在返回一个键列表......我将如何重新建立一个新字典,或配对主字典?任何帮助,将不胜感激。