开发者

Lazy-loading collections with restrictions in nHibernate

开发者 https://www.devze.com 2023-03-31 18:01 出处:网络
I have two simple entities with one-to-many relationship: public class Customer { public int Id { get; private set; }

I have two simple entities with one-to-many relationship:

public class Customer
{
    public int Id { get; private set; }
    public IList<Order> Orders { get; private set; }
}

public class Order
{
    public DateTime Date { get; set; }
}

Let's imagine, that there could be a lot of Orders for a particular Customer, and I want to see only last-month-orders.

Can I do this somehow if Orders property is lazy-loaded? I want something like:

var orders = customer.Orders.Where(x => x.Date > DateTime.Now.AddMonths(-1)开发者_JAVA百科).ToList();

but now nHibernate requests all the Orders on this line (SELECT * FROM Orders WHERE customerId='{0}'), and linq-restrictions are applied on the loaded data, but I want it to be applied to SQL (SELECT * FROM Orders WHERE customerId='{0}' AND date > '2011-07-31').

Is this achievable with nHibernate? Can Entity Framework do this?


Is this achievable with nHibernate? Can Entity Framework do this?

Unfortunately no. NHibernate and EF cannot handle this situation.


Try NHibernate 3, linq support there is much better than in 2nd version. Otherwise you can make request using criterions syntax.

0

精彩评论

暂无评论...
验证码 换一张
取 消