How to get dictionary values as a generic list

C#ListDictionary

C# Problem Overview


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 :-(

C# Solutions


Solution 1 - C#

How about:

var values = myDico.Values.ToList();

Solution 2 - C#

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();

Solution 3 - C#

You probably want to flatten all of the lists in Values into a single list:

List<MyType> allItems = myDico.Values.SelectMany(c => c).ToList();

Solution 4 - C#

Another variant:

List<MyType> items = new List<MyType>();
items.AddRange(myDico.Values);

Solution 5 - C#

My OneLiner:

var MyList = new List<MyType>(MyDico.Values);

Solution 6 - C#

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();

Solution 7 - C#

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

var items = myDico.Select(d=> d.Value).ToList();

Solution 8 - C#

Use this:

List<MyType> items = new List<MyType>()
foreach(var value in myDico.Values)
    items.AddRange(value);

The problem is that every key in your dictionary has a list of instances as value. Your code would work, if each key would have exactly one instance as value, as in the following example:

Dictionary<string, MyType> myDico = GetDictionary();
List<MyType> items = new List<MyType>(myDico.Values);

Solution 9 - C#

        List<String> objListColor = new List<String>() { "Red", "Blue", "Green", "Yellow" };
        List<String> objListDirection = new List<String>() { "East", "West", "North", "South" };

        Dictionary<String, List<String>> objDicRes = new Dictionary<String, List<String>>();
        objDicRes.Add("Color", objListColor);
        objDicRes.Add("Direction", objListDirection);

Solution 10 - C#

Another variation you could also use

MyType[] Temp = new MyType[myDico.Count];
myDico.Values.CopyTo(Temp, 0);
List<MyType> items = Temp.ToList();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFlorianView Question on Stackoverflow
Solution 1 - C#SlicedbreadView Answer on Stackoverflow
Solution 2 - C#VdesmedTView Answer on Stackoverflow
Solution 3 - C#SLaksView Answer on Stackoverflow
Solution 4 - C#shoma13View Answer on Stackoverflow
Solution 5 - C#Mathias VetschView Answer on Stackoverflow
Solution 6 - C#StackedView Answer on Stackoverflow
Solution 7 - C#CharithJView Answer on Stackoverflow
Solution 8 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 9 - C#sathisView Answer on Stackoverflow
Solution 10 - C#johnny 5View Answer on Stackoverflow