How do I get all the values of a Dictionary<TKey, TValue> as an IList<TValue>?

Noticed a lot of answer were quite old.

This will also work:

using System.Linq;

dict.Values.ToList();

Because of how a dictionary (or hash table) is maintained this is what you would do. Internally the implementation contains keys, buckets (for collision handling) and values. You might be able to retrieve the internal value list but you're better of with something like this:

IDictionary<int, IList<MyClass>> dict;
var flattenList = dict.SelectMany( x => x.Value );

It should do the trick ;) SelectMany flattens the result which means that every list gets concatenated into one long sequence (IEnumerable`1).


A variation on John's suggestion:

var flattenedValues = dict.Values.SelectMany(x => x);

If you need them in a list, you can of course call ToList:

var flattenedList = dict.Values.SelectMany(x => x).ToList();