EF CF中的主细节查询和通用存储库模式

时间:2012-05-23 10:19:43

标签: c# .net entity-framework-4 ef-code-first repository-pattern

我正在使用Generic存储库模式,如this article中所示。

这些是我的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的订单?我如何构建一个Expression来过滤细节作为OrderFromCustomer函数?
我也试过LinqKit但没有结果。

1 个答案:

答案 0 :(得分:0)

您可以将Where过滤器子句链接在一起,例如,按行完成,它将如下所示:

 orders = repository.Get(filter: q => q.Where(f => f.CustomerID == customerId).Where( n => n.Amount > 100);