NHibernate Linq Query Possible Bug

2 views
Skip to first unread message

Action Jackson

unread,
May 3, 2009, 4:10:21 PM5/3/09
to NHibernate Contrib - Development Group
I'm getting an error in which I cannot tell if it's a result of a
bug or just a badly written query on my part. Here is my query:

var q = from student in Session.Linq<Student>()
from user in Session.Linq<User>()
select student;

The Employee poco has a foreign key reference to a table containing
PersonInformation. The error I am getting is this:

Unable to cast object of type
'System.Linq.Expressions.ConstantExpression' to type
'System.Linq.Expressions.LambdaExpression'.

This exception is being thrown as a result of the following logic in
the HandleSelectManyCall method of the RootVisitor class :

//get the association path for the joined entity
var collectionSelector = (LambdaExpression)LinqUtil.StripQuotes
(call.Arguments[1]);

Apparently, for my query, LinqUtil.StripQuotes(call.Arguments[1]);
returns a ConstantExpression instead of the LambdaExpression that the
above code assumes it will be. Any ideas?

Chad Lee

unread,
May 3, 2009, 6:11:51 PM5/3/09
to nhc...@googlegroups.com
Creating a query with two roots like that is not supported by the Criteria API with which the LINQ implementation is based.

Fabio Maulo

unread,
May 3, 2009, 6:25:58 PM5/3/09
to nhc...@googlegroups.com
2009/5/3 Chad Lee <chad...@gmail.com>

is not supported by the Criteria API with which the LINQ implementation is based.

So far ;)

--
Fabio Maulo

Action Jackson

unread,
May 3, 2009, 6:33:28 PM5/3/09
to NHibernate Contrib - Development Group

Oh ok, fair enough. Are there any workarounds that you would
recommend? Or do I have to fall back to HQL?


On May 3, 5:25 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> 2009/5/3 Chad Lee <chadl...@gmail.com>

Chad Lee

unread,
May 4, 2009, 12:38:01 PM5/4/09
to nhc...@googlegroups.com
You can join on entity collections:


from student in Session.Linq<Student>()
from user in student.Users
select user;

if student has a collection of users.  Otherwise, yes, fall back to HQL.

Action Jackson

unread,
May 4, 2009, 1:04:54 PM5/4/09
to NHibernate Contrib - Development Group

Hi Chad,

Using the query technique you just specified, is there a way to
project? The specific problem I'm trying to solve is to not have the
entire User property be retrieved so that I don't have 20 extra,
unnecessary parameters being retrieved in a select statement. I'd
like the equivalent of the following SQL query:

select s.*, u.prop1, u.prop2 from student
join user u on s.userid = u.userid
where ....
> > > Fabio Maulo- Hide quoted text -
>
> - Show quoted text -

Chad Lee

unread,
May 4, 2009, 2:15:28 PM5/4/09
to nhc...@googlegroups.com
Yes, if student -> user is a 1:1 relationship, you can do:

from s in session.Linq<Student>()
select new { s.Prop1, s.User.Prop1, s.User.Prop2 }

or if it is one-to-many:

from s in session.Linq<Student>()
from u in s.Users
select new { s.Prop1, u.Prop1, u.Prop2 }

As far as projecting the entire student entity along with specific properties of a mapped association:

from s in session.Linq<Student>()
select new { s, s.User.Prop1, s.User.Prop2 }

That is not supported - again due to limitations with the Criteria API.
Reply all
Reply to author
Forward
0 new messages