我有以下(保持非常简单):
public class Customer
{
public int Id {get; set}
public IEnumerable<Reminder> Reminders {get; set}
public IEnumerable<Order> Orders {get; set}
}
public class Reminder
{
public int CustomerId {get; set}
public string Text {get; set}
}
public class Order
{
public int CustomerId {get; set}
public int CategoryId {get; set}
}
public class OrderDetails
{
public int OrderId {get; set}
public int ProductId {get; set}
}
我也使用通用存储库模式,我使用嵌套实体加载实体,如下所示:
public virtual T Get(int id, params Expression<Func<T, object>>[] include)
{
if (include.Any())
{
var set = include.Aggregate<Expression<Func<T, object>>, IQueryable<T>>
(dbset, (current, expression) => current.Include(expression));
return set.SingleOrDefault<T>(x => x.Id == id);
}
return dbset.Find(id);
}
使用Linq,我想加载一个客户,他所有的提醒和所有订单(及其详细信息)都有一个特定的CategoryId。所以我试着这样做:
var customer = customerRepository.Get(10, x => x.Reminders, x => x.Orders.Select(o => o.OrderDetails));
但上面加载了一切。如果我执行以下操作,则会抛出异常:
var customer = customerRepository.Get(10, x => x.Reminders, x => x.Orders.Where(c => c.CategoryId == categoryId).Select(o => o.OrderDetials));
那么我怎样才能真正只返回我需要的订单?