title: NHibernate: getting multiple entities with subset of child collection?
body: <p>My classes look something like this (I include only the relevant properties):</p>
  
  <pre><code>public class Order 
  {
     public virtual Customer Customer { get; set; }
     public virtual IEnumerable&lt;OrderLine&gt; OrderLines { get; set; }
  }
  
  public class OrderLine 
  {
     public virtual string Product { get; set; } // Simplified
  }
  </code></pre>
  
  <p>Now I What I want is to create a detached criteria that selects all orders for a given customer, and at the same time retrieves only the first 10 OrderLines for each order. The first part is easy:</p>
  
  <pre><code>Customer someCustomerObject = ...;
  var criteria = DetachedCriteria.For&lt;Order&gt;().Add(Restrictions.Eq("Customer", someCustomerObject);
  </code></pre>
  
  <p>But how do I instruct NHibernate to retrieve eagerly the first 10 orderlines for each order retrieved by the criteria above?</p>
  
  <p>I've tried using a Filter based on the following example (taken from Nhibernate documentation):</p>
  
  <pre><code>session.CreateFilter( lazyCollection, "").SetFirstResult(0).SetMaxResults(10).List();
  </code></pre>
  
  <p>But when I give <code>Order.OrderLines</code> to the <code>CreateFilter</code> method, it retrieves all orderlines first, and then afterwards retrieves the 10 first orderlines, which is not what I want. I also tried combining this with a call to <code>NHibernateUtil.Initialize</code> to no avail.</p>
  
  <p>How do I create a detached criteria for this problem? Or, if that is not entirely possible, how to I retrieve, for each order, the 10 first results only, without fetching the entire collection?</p>