有这个查询的答案
我正在尝试了解要在此LINQ段中放入什么
pair => pair.Key, pair => pair.Value
我确切的列表定义是这样的:
List<Dictionary<string, StockDetails>> myList = new List<Dictionary<string, StockDetails>>();
这是我尝试过的,但被标记为生成错误:
IDictionary<string, StockDetails> dictionary = myList.ToDictionary(myList => myList.Key, pair => myList.Value);
如前所述,我只是想了解此LINQ部分,以了解如何正确定义它才能成功构建:
myList => myList.Key, pair => myList.Value);
我的构建错误是
Error 7 'System.Collections.Generic.Dictionary<string,xx.Models.StockDetails>' does not contain a definition for 'Key' and no extension method 'Key' accepting a first argument of type 'System.Collections.Generic.Dictionary<string,xx.Models.StockTickerDetails>' could be found (are you missing a using directive or an assembly reference?)
xxxx
Error 9 'System.Collections.Generic.List<System.Collections.Generic.Dictionary<string,xx.Models.StockTickerDetails>>' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Collections.Generic.List<System.Collections.Generic.Dictionary<string,xx.Models.StockDetails>>'
could be found (are you missing a using directive or an assembly reference?)
谢谢你的帮助
你也可以尝试这样的事情:
IDictionary<string, StockDetails> result = myList.SelectMany(d => d).ToDictionary(e=>e.Key,e=>e.Value);
在SelectMany
将项目中的每个字典作为一个序列,它会产生的序列压扁成一个序列与你在你的字典,这样,调用该方法的结果是所有的元素IEnumerable<KeyValuePair<string, StockDetails>>
。然后,您可以调用之前引用ToDictionary
的答案之类的方法,以将结果序列转换为。Dictionary<string,StockDetails>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句