Florian Florian - 1 year ago 87
C# Question

How to get dictionary values as a generic list

I just want get a list from Dictionary values but it's not so simple as it appears !

here the code :

Dictionary<string, List<MyType>> myDico = GetDictionary();
List<MyType> items = ???

I try :

List<MyType> items = new List<MyType>(myDico.values)

But it does not work :-(

Answer Source

Off course, myDico.Values is List<List<MyType>>.

Use Linq if you want to flattern your lists

var items = myDico.SelectMany (d => d.Value).ToList();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download