I just want get a list from Dictionary values but it\'s not so simple as it appears !
here the code :
Dictionary> my
Another variation you could also use
MyType[] Temp = new MyType[myDico.Count];
myDico.Values.CopyTo(Temp, 0);
List<MyType> items = Temp.ToList();
Dictionary<string, MyType> myDico = GetDictionary();
var items = myDico.Select(d=> d.Value).ToList();
Going further on the answer of Slaks, if one or more lists in your dictionary is null, a System.NullReferenceException
will be thrown when calling ToList()
, play safe:
List<MyType> allItems = myDico.Values.Where(x => x != null).SelectMany(x => x).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();
You probably want to flatten all of the lists in Values
into a single list:
List<MyType> allItems = myDico.Values.SelectMany(c => c).ToList();
Another variant:
List<MyType> items = new List<MyType>();
items.AddRange(myDico.values);