views:

211

answers:

0

Hello,

My classes look something like this (I include only the relevant properties):

public class Order 
{
   public virtual Customer Customer { get; set; }
   public virtual IEnumerable<OrderLine> OrderLines { get; set; }
}

public class OrderLine 
{
   public virtual string Product { get; set; } // Simplified
}

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:

Customer someCustomerObject = ...;
var criteria = DetachedCriteria.For<Order>().Add(Restrictions.Eq("Customer", someCustomerObject);

But how do I instruct NHibernate to retrieve eagerly the first 10 orderlines for each order retrieved by the criteria above?

I've tried using a Filter based on the following example (taken from Nhibernate documentation):

session.CreateFilter( lazyCollection, "").SetFirstResult(0).SetMaxResults(10).List();

But when I give Order.OrderLines to the CreateFilter 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 NHibernateUtil.Initialize to no avail.

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?