How do I convert a .Net Dictionary<String,SomeClassType> to a List<SomeClassType>?


I have a .Net dictionary defined to be the following:

Dictionary<String, SomeClassType> foo;

Open in new window

How do I convert the "Values" portion of this to a

Open in new window

I would have thought that the following would work:

var foobar = foo.Values.ToList<SomeClassType>();

Open in new window

I get the following compiler error:
Error   14 'System.Collections.Generic.Dictionary<string,SomeClassType>.ValueCollection' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.ParallelEnumerable.ToList<TSource>(System.Linq.ParallelQuery<TSource>)' has some invalid arguments      

jxbmaSoftware ConsultantAsked:
Who is Participating?
Easwaran ParamasivamConnect With a Mentor Commented:
Dictionary<string, object> dict = new Dictionary<string, object>();
var arr = dict.Select(z => z.Value).ToList();

Open in new window


List<Foo> arr = new List<Foo>(dict.Values);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.