Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting dynamic to object

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.

like image 423
petko_stankoski Avatar asked May 07 '12 06:05

petko_stankoski


1 Answers

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.

like image 104
Jon Skeet Avatar answered Oct 04 '22 02:10

Jon Skeet