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.
Similar to dbaseman’s answer (and AKX’s comment) I’d use
Cast:You’ll need a
usingdirective for LINQ though:at the top of your file. It sounds like you haven’t got that if the
Selectmethod isn’t recognized.Note that this assumes that each value really is a
MyClassreference.EDIT: If you want to be able to access the values by index, I’d recommend using
ToList:While
ToArraywould 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 ofMyClassfrom each item, e.g.or possibly:
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.