title: Fluent NHibernate Querying: OrderBy column of joined table
body: <p>I've been struggling quite some time now with trying to query some stuff using NHibernate.
  I've managed to do some very simple queries, but now I'm trying to build some custom pagination through NHibernate.</p>
  
  <p>What I want to do is twofold.
  <strong>First (1)</strong> off, I want to be able to order my resultset based on a <strong>column from a joined table</strong>.
  Let's say I have a 'Person' table that has a one-to-one (actually many-to-one) reference with an 'Address' table.
  I want to query over the 'Person' table (using its fields), but want to sort using fields from the 'Address' table.</p>
  
  <p>How do I do this? I've tried the following two ways</p>
  
  <pre><code>var resultset = GetCurrentSession().QueryOver&lt;Person&gt;()
                     .Where(x =&gt; x.Name == "...")
                     .JoinQueryOver&lt;Address&gt;(x =&gt; x.Address)
                     .OrderBy(x =&gt; x.HouseNumber).Desc
                     .Skip(...)
                     .Take(...)
                     .List();
  // ==&gt; could not execute query
  
  Person person = null;
  Address address = null;
  var resultset = GetCurrentSession().QueryOver&lt;Person&gt;()
                     .JoinAlias(() =&gt; person, () =&gt; address.Persons)
                     .Where(() =&gt; person.Name == "...")
                     .OrderBy(() =&gt; address.HouseNumber).Asc
                     .Skip(...)
                     .Take(...)
                     .List();
  // ==&gt; could not resolve property: person of: Person
  </code></pre>
  
  <p>My <strong>second (2)</strong> question is, I would like to split the ordering up.
  Based on conditional statements, I want to add a different OrderBy.
  Can I just do it like so?</p>
  
  <pre><code>var query = GetCurrentSession().QueryOver&lt;Person&gt;()
                 .Where(x =&gt; x.Name == "...")
                 .JoinQueryOver&lt;Address&gt;(x =&gt; x.Address);
  
  if(foo)
  {
      query = query.OrderBy(() =&gt; address.HouseNumber).Asc
  }
  else if (bar)
  {
      query = query.OrderBy(() =&gt; address.Street).Desc
  }
  
  var resultset = query                    
                     .Skip(...)
                     .Take(...)
                     .List();
  </code></pre>
  
  <p>Thanks a lot!</p>