cannot project entities in Linq 2 NHibernate
-
14-04-2021 - |
Frage
I'm working with NHibernate 2 in a .Net project and I'm using the Linq2NHibernate provider. This simple query
var result = from d in session.Linq<Document>()
where d.CreationYear == 2010
select d.ChildEntity).ToList();
throws an exception telling me that is impossible to cast ChildEntity type do Document type. Why is that? I also tried to translate it in query methods, having
session.Linq<Document>()
.where(d=>d.CreationYear == 2010)
.select(d=>d.ChildEntity)
.ToList();
Isn't the select method supposed to project an IQueryble into a IQueryble, beeing TResult!=T ?
Lösung
The old Linq provider is extremely limited and has been unmaintained for several years.
I suggest that you upgrade to the latest stable NHibernate (3.2), which has a much better (and integrated) Linq provider.
Andere Tipps
Try this:
var result = (from d in session.Linq<Document>()
where d.CreationYear == 2010
select new ChildEntityType
{ /* here just do a simple assignments for all ChildEntityType fields
d.ChildEntity */ } ).ToList();
Yes, this could look quite stupid, but linq2nhibernate sometimes behave very strange, when you try to select just an object.
can you try this:
session.Linq<Document>()
.Where(d=>d.CreationYear == 2010)
.Select(d=>d.ChildEntity)
.ToList<T>(); //where T is typeof(ChildEntity)