Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top