Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate 3 Linq - inner joins

I'm testing out nhibernate 3 CR, but fails to create the following SQL using Linq:

select   *
     from        Users               as {user}
     inner join  Test                as test  on test.UserId   = user.Id
     inner join  Release             as release on release.TestId = test.TestId
     where Release.Status = 1
     order by    count(release.Status) desc;

I have not got so far, my current code is like this and gives me something complete different:

var users = from user in Session.Query<User>()
            join test in Session.Query<Test>() on user.Id equals test.User.Id
            join release in Session.Query<Release>() on test.Id equals release.Test.Id
            where release.Status == 1
            orderby release.Status
            descending 
            select user;

Is there any resources on how to use inner joins with linq? And what should I do with:

order by    count(release.Status)

Is this something that should be done with QueryOver instead?

like image 856
bondehagen Avatar asked Nov 25 '10 10:11

bondehagen


1 Answers

First, define relationships in your model instead of trying to join by id.

Then you'll be able to do this:

from release in session.Query<Release>()
where release.Status == 1
select release.Test.User

All that's missing is the orderby, which I don't think is correct (you are trying to order by an aggregate, but you're not specifying a group by)

like image 119
Diego Mijelshon Avatar answered Sep 21 '22 05:09

Diego Mijelshon