How to convert a list of dictionaries into IDictionary “C#”
Asked Answered
A

1

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

Assail answered 24/3, 2015 at 19:7 Comment(6)
So, you have a list of dictionaries and want to convert that list into a single consolidated dictionary?Negativism
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...Greenroom
I have added my errors as requested. Thanks for the feedback so farAssail
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?)Tenerife
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.Assail
Sounds to me like you need some help understanding Lambda expressions.Declaim
D
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>.

Deuno answered 24/3, 2015 at 19:40 Comment(2)
Sorry neither answer provides how to create my IDictionary not Dictionary result.Assail
Dictionary is IDictionary basicallyQuicken

© 2022 - 2024 — McMap. All rights reserved.