我有关于Linq/Lambda的问题以及以下问题:
我有两个词典,主要和次要......这两个词典被定义为Key = string,Value = int.如果KEYS与辅助字典相交,我需要修剪主字典.
即:
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]
Run Code Online (Sandbox Code Playgroud)
我的尝试:
resultDict = primaryDict.Keys.Intersect(secondaryDict.Keys).ToDictionary(t => t.Key, t.Value);
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为primaryDict.Keys.Intersect返回一个键列表...我将如何重新建立一个新词典,或者配对主词典?任何帮助,将不胜感激.