1

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 ?

themarcuz
  • 2,573
  • 6
  • 36
  • 53

3 Answers3

1

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.

Serge SB
  • 61
  • 1
  • 2
0

can you try this:

session.Linq<Document>()
   .Where(d=>d.CreationYear == 2010)
   .Select(d=>d.ChildEntity)
   .ToList<T>();     //where T is typeof(ChildEntity)
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
0

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.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154