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 :-(
In C#, Dictionary is a generic collection which is generally used to store key/value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of Dictionary is, it is generic type. Dictionary is defined under System.
A relatively safe, simple, yet high performance technique for using lists as dictionary keys. Using collections as dictionary keys is sometimes necessary, but it can be a performance killer and unsafe. Here's how to make it faster and safer.
Dictionaries can't have null keys.
How about:
var values = myDico.Values.ToList();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With