“No se puede instanciar” Raise NHibernate.QueryException Por combinado de consultas LINQ

StackOverflow https://stackoverflow.com/questions/3998247

  •  10-10-2019
  •  | 
  •  

Pregunta

La ejecución de la siguiente declaración NHibernate.Linq plantea una NHibernate.QueryException "could not instantiate: Reservation001.Services.ReservationDto" que contiene una excepción InvalidCast interior ( "objeto debe implementar IConvertible."):

var inOneStep = (from r in session.Linq<Models.ReservationHeader>()
    select new ReservationDto(r.Current));
return inOneStep;

Sin embargo, después de la división de la anterior en dos consultas, con ToList () llamada en los resultados de la primera, la multa ejecuta el código.

var step1 = (from r in session.Linq<Models.ReservationHeader>()
     select r).ToList();
var step2 = from z in step1
     select new ReservationDto(z.Current);
return step2;

¿Por qué elevar la versión única sentencia una excepción?

Gracias,
Ben

¿Fue útil?

Solución

The reason that the first one does not work is because the whole query is getting sent to NHibernate, and (as the exception tells you) NHibernate expects something with ReservationDto to be IConvertible.

The two step process avoids this error, because by calling "ToList()" you cause the query to execute in NHibernate immediately without involving ReservationDto, and returning an object collection. Your second step is then simply operating on an object collection, and since NHibernate is no longer involved, you avoid the error.

In general, Linq uses delayed execution, with a few functions (such as ToList() ) forcing immediate evaluation. See http://devlicio.us/blogs/derik_whittaker/archive/2008/04/07/linq-and-delayed-execution.aspx

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top