C# – How to get all the values of a Dictionary as an IList

.net-3.5cdictionaryilist

I have a the following dictionary:

IDictionary<int, IList<MyClass>> myDictionary 

and I am wanting to get all the values in the dictionary as an IList….


Just to add a bit of a background as to how I've gotten into this situation….

I have a method that gets me a list of MyClass. I then have another method that converts that list into a dictionary where they key is the id for MyClass. Later on…and without access to that original list…I'm needing to obtain the original ungrouped list of MyClass.


When I pass myDictionary.Values.ToList() to a method that takes an IList I get a compile error that says that it can't convert from

System.Collections.Generic.List<System.Collections.Generic.IList<MyClass>> 

to:

System.Collections.Generic.IList<MyClass>

Now, I can understand that its gone and added each of the groups of IList to the new list as separate elements of the list….but in this instance its not really what I'm after. I just want a list of all the values in the entire dictionary.

How then can I get what I'm after without looping through each of the key values in the dictionary and creating the list I want?

Best Answer

Noticed a lot of answer were quite old.

This will also work:

using System.Linq;

dict.Values.ToList();