Converting dynamic to object

petko_stankoski picture petko_stankoski · May 7, 2012 · Viewed 17.1k times · Source

Here is my code:

MyClass here = new MyClass();
IEnumerable<MyClass> vats = (IEnumerable<MyClass>)here.All();

The All() method returns IEnumerable<dynamic>. I want to convert it to IEnumerable<MyClass>. The line above doesn;t work, it says Unable to cast object of type 'd__15' to type 'System.Collections.Generic.IEnumerable`1[MyClass]'.

I also tried:

 IEnumerable<MyClass> vats = here.All() as IEnumerable<MyClass>;

but it returns null.

Answer

Jon Skeet picture Jon Skeet · May 7, 2012

Similar to dbaseman's answer (and AKX's comment) I'd use Cast:

IEnumerable<MyClass> vats = here.All().Cast<MyClass>();

You'll need a using directive for LINQ though:

using System.Linq;

at the top of your file. It sounds like you haven't got that if the Select method isn't recognized.

Note that this assumes that each value really is a MyClass reference.

EDIT: If you want to be able to access the values by index, I'd recommend using ToList:

List<MyClass> vats = here.All().Cast<MyClass>().ToList();

While ToArray would work too, I personally favour lists over arrays in most cases, as they're rather more flexible.

EDIT: It sounds like your results are actually full of ExpandoObject. You'll need to create a new instance of MyClass from each item, e.g.

List<MyClass> vats = here.All()
                         .Select(item => new MyClass(item.Name, item.Value))
                         .ToList();

or possibly:

List<MyClass> vats = here.All()
                         .Select(item => new MyClass {
                                     Name = item.Name,
                                     Value = item.Value,
                                 })
                         .ToList();

That's just an example, which I wouldn't expect to work straight away - we can't do any better than that as we know nothing about how your results are actually being returned.

It does sound like you're in over your head here, I'm afraid.