NHibernate caching not working for anonymous type

459 views Asked by At

I am trying to get the following query to work:

Session.Linq<FooBar>()
    .SetCachable(true)
    .SetCacheRegion("foobar")
    .Select(x => new Baz(x.Foo, x.Bar))
    .ToList();

This works when caching is turned off, but with caching enabled I receive the following exception:

System.InvalidCastException: Unable to cast object of type 'Baz' to type 'System.Object[]'.

The rest of the stack trace is:

at NHibernate.Cache.StandardQueryCache.Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, Boolean isNaturalKeyLookup, ISessionImplementor session)
at NHibernate.Loader.Loader.PutResultInQueryCache(ISessionImplementor session, QueryParameters queryParameters, IType[] resultTypes, IQueryCache queryCache, QueryKey key, IList result)
at NHibernate.Loader.Loader.ListUsingQueryCache(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) 

Anyone know if this is an NHibernate limitation or am I doing something wrong?

1

There are 1 answers

0
dmonlord On BEST ANSWER

LinqToNHibernate is not a full implementation for querying data.

Use CriteriaQuery (about 95% coverage off all that is possible to query), or HQL (100% coverage).