0

我正在使用通用存储库模式,如本文所示

这些是我的 POCO 课程:

public class Order
{
    public int ID { get; set; }
    public int CustomerID {get; set;}

    [InverseProperty("Order")]
    public virtual List<OrderDetail> OrderDetail {get; set;}

    public static Expression<Func<Order, bool>> OrdersFromCustomer(decimal customerId)
    {
        return f => f.CustomerID == customerId;
    }
}

public class OrderDetail
{
    public int OrderID { get; set;}
    public int ID { get; set;}
    public int ItemID { get; set;}
    public int Amount { get; set;}

    [ForeignKey("OrderID")]
    public virtual Order { get; set;}
}

因此,当在我的程序中我想获得客户的所有订单时,我可以这样做:

using (MyDbContext context = new MyDbContext())
{
    MyRepository repository = new MyRepository(context);
    var orders = repository.Get(Order.OrdersFromCustomer(25));
}

它很好用,但我有一个问题:如果我想要所有金额大于 100 的订单?作为 OrderFromCustomer 函数,我如何构建一个表达式来过滤详细信息?
我也尝试过使用 LinqKit,但没有结果。

4

1 回答 1

0

您可以将 Where 过滤器子句链接在一起,例如,这样做如下所示:

 orders = repository.Get(filter: q => q.Where(f => f.CustomerID == customerId).Where( n => n.Amount > 100);
于 2012-06-08T14:54:56.023 回答