如何将字典列表转换为 IDictionary “C#”
How to convert a list of dictionaries into IDictionary “C#”
有一个query
的回答
我正在尝试了解将什么放入此 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
方法,就像你之前 quote 的答案一样,目的是将结果序列转换为 Dictionary<string,StockDetails>
.
有一个query
的回答我正在尝试了解将什么放入此 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
方法,就像你之前 quote 的答案一样,目的是将结果序列转换为 Dictionary<string,StockDetails>
.