Hm,
that's a strange one. Anonymous objects typically work. Also anonymous 
objects are not cached in in the session.
Maybe query caching? Is this enabled? 2nd level cache?

Did you try to execute Select before Order?

You are using the latest Nhibernate 4.0.4?

best regards,
Peter

Am Mittwoch, 3. Februar 2016 13:48:00 UTC+1 schrieb Felipe Oriani:
>
> Hello guys! 
>
> We have a web application which has some methods defined by user and 
> executed by Codedom (CSharpCodeProvider) between our code. Our customer has 
> programmers that can adpts business rules over the project and execute some 
> queries on the scope of execution. We have a simple query like below 
> execution on Codedom:
>
> 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 it's ok. If we 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 without any problems. We 
> have no idea of what causes the error.
>
> Its important to say that this code is running in an CSharpCodeProvider 
> environment, but we don't know if it can make a difference. We also tried 
> everything, for sample, removing Where(), OrderBy(), changing the Select() 
> return, etc... but no progress.
>
> We think the problem is with Anonnymous objects return because if we do a 
> DTO to return a result, it executes ok. If we just return a single value 
> using .Select(x => x.EmployeeId), it executes ok too. We do not want to 
> force the user defined a DTO for each custom query they can do. 
>
> We just realize that if I add the following property to the annon object, 
> the query works everytime:
>
> Rnd = (new Random().Next(1, 999))
>
> (it's a terrible solution but it worked)
>
> So, a cache issue maybe?
>
> Thank you.
>
> -- 
> ______________________________________
> Felipe B Oriani
> [email protected] <javascript:>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to