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?

Comments

  • It's hard to say without knowing what your driver is doing. Have you tried attaching a debugger and enabling break on exceptions?
  • Yes I have. Initially, no exception was thrown in my code. However, I disabled Just my code and this gave the stack trace below. My guess is that something is not correctly refreshed because the type names of the different queries are equal even though the real types are not. I.e. they are both named <>f__AnonymousType0, but because the names of the properties differ, the types aren't the same so that is why the cast fails.
    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]]'.
       at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
    
    Inner exception stack trace:
    
    nhibernate.dll!NHibernate.Linq.DefaultQueryProvider.Execute<System.Collections.Generic.IEnumerable<<>f__AnonymousType0<string>>>(System.Linq.Expressions.Expression expression) + 0x5d bytes	
    nhibernate.dll!Remotion.Linq.QueryableBase<<>f__AnonymousType0<string>>.GetEnumerator() + 0x81 bytes	
    mscorlib.dll!System.Collections.Generic.List<object>.List(System.Collections.Generic.IEnumerable<object> collection) + 0x172 bytes	
    System.Core.dll!System.Linq.Enumerable.ToList<object>(System.Collections.Generic.IEnumerable<object> source) + 0x3b bytes	
    LINQPad.exe!LINQPad.UI.ExplorerGrid.UpdateDataSourceToUse() + 0x3d2 bytes	
    LINQPad.exe!LINQPad.UI.ExplorerGrid.CustomDataSource.set(object value) + 0x3f bytes	
    LINQPad.exe!LINQPad.UI.ExplorerGrid.Display(object o, LINQPad.Extensibility.DataContext.GridOptions options) + 0x4cc bytes	
    LINQPad.exe!LINQPad.Extensibility.DataContext.DataContextDriver.DisplayObjectInGrid(object objectToDisplay, LINQPad.Extensibility.DataContext.GridOptions options) + 0x31 bytes	
    Titan.LinqPad.dll!Titan.LinqPad.TitanDomainDriver.DisplayObjectInGrid(object objectToDisplay, LINQPad.Extensibility.DataContext.GridOptions options) Line 260 + 0xe bytes	C#
    LINQPad.exe!LINQPad.Extensions.Explore<object>(object o, LINQPad.Extensibility.DataContext.GridOptions options) + 0xc2 bytes	
    LINQPad.exe!LINQPad.Extensions.Explore<object>(object o, string panelTitle) + 0x9d bytes	
    LINQPad.exe!LINQPad.Extensions.Dump<object>(object o, string description, int? depth, bool toDataGrid, System.Action<LINQPad.ClickContext> onClick) + 0xac2 bytes	
    LINQPad.exe!LINQPad.Extensions.Dump<object>(object o, string description, int? depth) + 0x7c bytes	
    LINQPad.exe!LINQPad.Extensions.Dump<object>(object o) + 0x7e bytes	
    query_uhhxjq.dll!UserQuery.RunUserAuthoredQuery() Line 31 + 0x27b bytes	C#
    [Native to Managed Transition]	
    LINQPad.exe!LINQPad.ExecutionModel.ClrQueryRunner.Run() + 0x407 bytes	
    LINQPad.exe!LINQPad.ExecutionModel.Server.RunQuery(LINQPad.ExecutionModel.QueryRunner runner) + 0x2f5 bytes	
    LINQPad.exe!LINQPad.ExecutionModel.Server.StartQuery(LINQPad.ExecutionModel.QueryRunner runner) + 0x2f3 bytes	
    LINQPad.exe!LINQPad.ExecutionModel.Server.ExecuteClrQuery.AnonymousMethod__1d() + 0x68 bytes	
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x6f bytes	
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes	
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 bytes	
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 bytes	
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes	
    [Native to Managed Transition]	
    [Appdomain Transition]	
    [Native to Managed Transition]
  • Maybe nHibernate is type-caching something inappropriately. You can tell LINQPad to reset the application domain between queries in Edit | Preferences | Advanced. If this fixes it, at least you know where to look.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!