codecompleting
codecompleting

Reputation: 9611

How to order by columns from an inner join with QueryOver?

Say I have a query like:

return NHibernateHelper.Session.QueryOver<SomeEntity>()
    .JoinQueryOver(x => x.Entity2)
    .JoinQueryOver(x => x.Entity3)
    .Where(x = x.Id > 10)
    .OrderBy( ????  )
    .List<SomeEntity>();

Now say I want to order by Entity3.SortOrder column, how can I do that?

Upvotes: 2

Views: 1255

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

Entity2 e2Alias = null;
Entity3 e3Alias = null;
SomeEntity s = null;
return NHibernateHelper.Session.QueryOver<SomeEntity>()
    .JoinAlias(() => s, () => e2Alias.SomeEntityReference) //here you need to specify the other  side of the relation in the other entity that refernces th SomeEntity
    .JoinAlias(() => s, () => e3Alias.SomeEntityReference)
    .Where(() => s.Id > 10)
    .OrderBy( () => e3Alias.SortOrder).Asc //or Desc
    .List<SomeEntity>(); 

Upvotes: 3

Related Questions