InvalidCastException with anonymous classes with custom driver
I have a custom driver implementation and I get an InvalidCastException when using anonymous classes. Say I have the following query:
from product in Products
select new { Name1 = product.Name }
This works OK. Now, I run the following query (the name of the first property is different):
from product in Products
select new { Name2 = product.Name }
Now I get the following exception:
InvalidCastException: Unable to cast object of type 'System.Linq.EnumerableQuery`1[<>f__AnonymousType0`1[System.String]]' to type 'System.Collections.Generic.IEnumerable`1[<>f__AnonymousType0`1[System.String]]'.
When I change the number of properties of the anonymous type, the exception goes away (I think this is due to the anonymous type being names f__AnonymousType0`2; if I then again change the name of one of the properties, the exception occurs again).
I do not get this exception when I use the LINQ to SQL driver so I guess I'm doing something wrong in my driver. How can I solve this?
from product in Products
select new { Name1 = product.Name }
This works OK. Now, I run the following query (the name of the first property is different):
from product in Products
select new { Name2 = product.Name }
Now I get the following exception:
InvalidCastException: Unable to cast object of type 'System.Linq.EnumerableQuery`1[<>f__AnonymousType0`1[System.String]]' to type 'System.Collections.Generic.IEnumerable`1[<>f__AnonymousType0`1[System.String]]'.
When I change the number of properties of the anonymous type, the exception goes away (I think this is due to the anonymous type being names f__AnonymousType0`2; if I then again change the name of one of the properties, the exception occurs again).
I do not get this exception when I use the LINQ to SQL driver so I guess I'm doing something wrong in my driver. How can I solve this?
Comments