I have a simple query like below:
var employeeTeam = Session.Query<EmployeeTeam>()
.Where(x => x.StartEffective <= competency.FinalDate && // competency.FinalDate is a DateTime
employeesIds.Contains(x.EmployeeId)) // employeeIds is a List<long>
.OrderByDescending(x => x.StartEffective)
.Select(x => new
{
x.EmployeeId,
x.StartEffective,
x.Team
}).ToList();
It successfully runs once, but when executed in the second time(or third, fourth and so son) it throws an invalid cast exception like:
Fatal Error:System.InvalidCastException: Cannot convert type 'System.Linq.EnumerableQuery`1[<>f__AnonymousType0`3[System.Int64,System.DateTime,Team]]' to 'System.Collections.Generic.IEnumerable`1[<>f__AnonymousType0`3[System.Int64,System.DateTime,Team]]'. in NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
Rest of the stack trace supressed for bravety.
The query is always executed in database before the error. It returns no records, but its is ok. If I rebuild the solution and run again, the query is executed in first time again, and then start throwing the exception each other time I run it. Other queries runs everytime w/o any problems. I have no idea of what causes the error.
Its important to say that this code is running in an CSharpCodeProvider environment, but I don't know if it can make a difference.
UPDATE
It happens even with the most simple form of the query:
var employeeTeam = Session.Query<EmployeeTeam>()
.Select(x => new
{
x.Id
}).ToList();
It runs ok for the first time only. But if I change the annon object from { x.Id }
to { x.TeamId }
, for example, it runs ok in the first time, then the exceptions occurs again.
UPDATE 2
I just realize that if I add the following property to the annon object, the query works everytime:
Rnd = (new Random().Next(1, 999))
So, a cache issue maybe?
UPDATE 3
I updated the NHibernate from 3.3
to 4.0.0.4
and it solves almost all problems except by one query:
var query = session.Query<Holiday>()
.Select(x => new {
HoliDayCities = x.City.Select(c => c.Id).ToList(),
HoliDayStates = x.State.Select(s => s.Id).ToList(),
Date = new DateTime((int)(x.Year.HasValue ? x.Year : competencia.InitialDate.Year), (int)x.Month, (int)x.Day)
}).ToList();
Error message:
GenericADOException: The value "{ HoliDayCities = System.Collections.Generic.List`1[System.Int64], HoliDayStates = System.Collections.Generic.List`1[System.Int64], Date = 01/02/2015 00:00:00 }" is not "<>f__AnonymousType1`3[System.Collections.Generic.List`1[System.Int64],System.Collections.Generic.List`1[System.Int64],System.DateTime]" and cannot be used on this collection. Parameter name: value
If I add the Rnd()
function on Select
scope as I mentioned before, it works fine. The problem occurres only with anonymous object.
OrderByDescending(x => x.StartEffective)
to right after the projection? – GilreathOrderByDescending
the problem remains. – AuschwitzISession
object. It's supposed to be short lived and by the code above it's not clear on what the lifetime is exactly. – Beaston.ToList()
avoids the exception. I think the problem is in the ToList as well, because the query is executed in database actually, to problem comes after. – AuschwitzemployeeTeam
never gets a value, the exception is on.ToList()
actually. I don't know exactly how can I debug a NH method, but I will try to figure out, it will be helpful indeed. Have you any resource of how I do that ? – AuschwitzIEntityPersister
) and that the reference version is wrong and stuff... I just don't now how to solve this shitty issue. I'm sry. Thank you .net to suck at references (y) – AuschwitzSelect
method. If the query brings the same result, it crashes, but if you add a random number hard-coded in a property of that annon object, it works. Got it? – Auschwitz.ToList()
before.Select()
like in the line you commented (lines 23, 42) or I use a class in select like.Select(x => new SomeClass() { a = x.a ... })
. Unfortunately, I moved up from this issue to other stuff and I won't getting back to it soon.. – Auschwitz