8

There is this answer of a query

I am trying to understand what to put into this LINQ segment

pair => pair.Key, pair => pair.Value

My exact List definition is this:

List<Dictionary<string, StockDetails>> myList = new List<Dictionary<string, StockDetails>>();

This what I attempted but it got flagged for build errors:

IDictionary<string, StockDetails> dictionary = myList.ToDictionary(myList => myList.Key, pair => myList.Value);

As said, I am just trying to understand this LINQ part in how to properly define it for a successful build:

myList => myList.Key, pair => myList.Value);

My build errors are

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?)

Thanks for any help

Community
  • 1
  • 1
heavy rocker dude
  • 2,271
  • 8
  • 33
  • 47
  • So, you have a list of dictionaries and want to convert that list into a single consolidated dictionary? – poke Mar 24 '15 at 19:12
  • I've deleted my answer because I misread the question. Please include the error message (which I suspect is due to introducing `myList` when it's already in scope) and tell us what you're trying to achieve. I thought you had a list of key/value pairs, but now you appear to have a list of dictionaries... – Jon Skeet Mar 24 '15 at 19:16
  • I have added my errors as requested. Thanks for the feedback so far – heavy rocker dude Mar 24 '15 at 19:26
  • You say what you tried but not what you want to do. What do you want to do with your list of dictionaries? Did you mean to have a list of **key value pairs** instead? (As in the title?) – Timothy Shields Mar 24 '15 at 19:34
  • I am just trying to understand how to create IDictionary from myList. I am trying to figure how to implement with this LINQ piece. I mean how do I define this e or d values as octavio specified.Nothing mentioned so far does not work. – heavy rocker dude Mar 24 '15 at 20:46
  • Sounds to me like you need some help understanding [Lambda expressions](https://msdn.microsoft.com/en-us/library/bb397687.aspx). – BJ Myers Mar 24 '15 at 21:20

1 Answers1

16

Also you could try something like this:

IDictionary<string, StockDetails> result = myList.SelectMany(d => d).ToDictionary(e=>e.Key,e=>e.Value);

The SelectMany will project each dictionary as a sequence and it will flatten the resulting sequences into one sequence with all the elements that you have in your dictionaries, so, the result of calling that method is a IEnumerable<KeyValuePair<string, StockDetails>>. Then you can call the ToDictionary method like the answer that you quote before with the intention to convert the resulting sequence in a Dictionary<string,StockDetails>.

Community
  • 1
  • 1
ocuenca
  • 38,548
  • 11
  • 89
  • 102