title: NHibernate select a list of objects with related child objects with QueryOver
body: <p>I'm having trouble with something that is probably very simple.
  In my database I have the following tables:</p>
  
  <pre><code>tblOrder
  -----------------
  Id
  OrderStatusId
  
  tblOrderStatus
  -----------------
  Id
  Name
  </code></pre>
  
  <p>And I have made the following mappings in my project:</p>
  
  <pre><code>[Class(NameType = typeof(Order), Table = "tblOrder")
  public class Order {
      [Id(-2, Name = "Id")]
      [Generator(-1, Class = "native")]
      public virtual long Id { get; set; }
  
      [ManyToOne]
      public virtual OrderStatus Status { get; set; }
  }
  
  [Class(NameType = typeof(OrderStatus), Table = "tblOrderStatus")]
  public class OrderStatus {
      [Id(-2, Name = "Id")]
      [Generator(-1, Class = "native")]
      public virtual long Id { get; set; }
  
      [Property]
      public virtual string Name { get; set; }
  }
  </code></pre>
  
  <p>The query should return a <code>IList&lt;OrderSummary&gt;</code>. I want the class <code>OrderSummary</code> to have a property <code>Status</code> where <code>Status</code> is an object with an <code>Id</code> and a <code>Name</code> property. This could be either with a <code>KeyValuePair</code> or of type <code>OrderStatus</code> (whichever is best and works). Fetching the orders is not a problem but adding the <code>OrderStatus</code> as an object with said properties is the part I'm having trouble with.
  I also need to return the result of the query as JSON to the client.</p>
  
  <p><code>OrderSummary</code> should look like this:</p>
  
  <pre><code>public class OrderSummary {
      public long Id { get; set; }
      public OrderStatus Status { get; set; }
  }
  </code></pre>
  
  <p>In my first version <code>OrderSummary</code> had separate properties for <code>OrderStatusId</code> and <code>OrderStatusName</code>. This works but I'm trying to avoid these separate properties. 
  I have also tried this with <code>SelectSubQuery</code> but this returns an error because it returns more than one field in a subquery.</p>
  
  <p>----------------------------------- UPDATE -----------------------------</p>
  
  <p>Following Fredy Treboux's advice I changed my query using <code>Eager</code> which result in the following query:</p>
  
  <pre><code>var query = session.QueryOver&lt;OrderStatus&gt;
      .Fetch(o =&gt; o.Status).Eager
      .JoinAlias(o =&gt; o.Status, () =&gt; statusAlias, JoinType.LeftOuterJoin);
  </code></pre>
  
  <p>The problem is, I found out, is not selecting the data but how to convert the retrieved <code>Status</code> and assign it to OrderSummary.Status? I have tried the following:</p>
  
  <pre><code>OrderSummary orderAlias = null;
  query.SelectList(list =&gt; list
      .Select(o =&gt; o.Id).WithAlias(() =&gt; orderAlias.Id)
      .Select(() =&gt; statusAlias).WithAlias(() =&gt; orderAlias.Status)
  ).TransformUsing(Transformer.AliasToBean&lt;OrderSummary&gt;());
  </code></pre>
  
  <p>-------------------------------- ANSWER ----------------------------------</p>
  
  <p>As I said in my last edit, the problem does not seem to be the actual selection of <code>OrderStatus</code> but returning it to the client. So I thought it was my lack of knowledge of NHibernate instead it was as simple as adding the <code>[JsonObject]</code> attribute to the <code>OrderStatus</code> class. How silly of me.</p>
  
  <p>I have changed my query to the following:</p>
  
  <pre><code>Order orderAlias = null;
  OrderSummary orderSummary = null;
  OrderStatus statusAlias = null;
  var query = session.QueryOver&lt;Order&gt;(() =&gt; orderAlias)
      .JoinAlias(() =&gt; orderAlias.Status, () =&gt; statusAlias, JoinType.LeftOuterJoin);
  
  query = query
      .Select(
          Projections.ProjectionList()
              .Add(Projections.Property(() =&gt; orderAlias.Id).WithAlias(() =&gt; orderSummary.Id))
              .Add(Projections.Property(() =&gt; orderAlias.Status).WithAlias(() =&gt; orderSummary.Status)
      );
  Result = query.TransformUsing(Tranformers.AliasToBean&lt;OrderSummary&gt;())
      .List&lt;OrderSummary&gt;()
      .ToList();
  </code></pre>