Linq:嵌套子查询中的动态 Where 子句

本文关键字:动态 Where 子句 查询 嵌套 Linq | 更新日期: 2023-09-27 18:34:36

过去,我通过向 Linq 查询动态添加过滤器来处理可选的搜索条件,如下所示:

public IEnumerable<Customer> FindCustomers(string name)
{
    IEnumerable<Customer> customers = GetCustomers();
    var results = customers.AsQueryable();
    if (name != null)
    {
        results = results.Where(customer => customer.Name == name);
    }
    results = results.OrderBy(customer => customer.Name);
    return results;
}

或者类似地使用谓词,您基本上只是将 lambda 从 Where 移动到Func<>(如果使用 LinqToEntities,则or Expression<Func<>>(,如下所示:

public IEnumerable<Customer> FindCustomers(string name)
{
    Func<Customer, bool> searchPredicate = customer => true;
    if (name != null)
    {
        searchPredicate = customer => customer.Name == name;
    }
    IEnumerable<Customer> customers = GetCustomers();
    var results = customers
        .Where(searchPredicate)
        .OrderBy(customer => customer.Name);
    return results;
}

但是,当 Where 子句隐藏在嵌套子查询中的某个位置时,我不知道如何做类似的事情。请考虑以下(编造的(方案:

public class Customer
{
    public string Name;
    public int MaxOrderItemAmount;
    public ICollection<Order> PendingOrders;
    public ICollection<OrderItem> FailedOrderItems;
    public ICollection<Order> CompletedOrders;
}
public class Order
{
    public int Id;
    public ICollection<OrderItem> Items;
}
public class OrderItem
{
    public int Amount;
}
public IEnumerable<OrderItem> FindInterestingOrderItems(
    bool onlyIncludePendingItemsOverLimit)
{
    var customers = GetCustomersWithOrders();
    // This approach works, but yields an unnecessarily complex SQL
    // query when onlyIncludePendingItemsOverLimit is false
    var interestingOrderItems = customers
        .SelectMany(customer => customer.PendingOrders
            .SelectMany(order => order.Items
                .Where(orderItem => onlyIncludePendingItemsOverLimit == false
                    || orderItem.Amount > customer.MaxOrderItemAmount))
            .Union(customer.FailedOrderItems)
        );
    // Instead I'd like to dynamically add the Where clause only if needed:
    Func<OrderItem, bool> pendingOrderItemPredicate = orderItem => true;
    if (onlyIncludePendingItemsOverLimit)
    {
        pendingOrderItemPredicate =
            orderItem => orderItem.Amount > customer.MaxOrderItemAmount;
       // PROBLEM: customer not defined here  ^^^
    }
    interestingOrderItems = customers
        .SelectMany(customer => customer.PendingOrders
            .SelectMany(order => order.Items
                .Where(pendingOrderItemPredicate)
            .Union(customer.FailedOrderItems)))
        .OrderByDescending(orderItem => orderItem.Amount);
    return interestingOrderItems;
}

显然,这次我不能只是将lambda移动到Func<>,因为它包含对由查询的更高级别部分定义的变量(customer(的引用。我在这里错过了什么?

Linq:嵌套子查询中的动态 Where 子句

这是有效的:在单独的函数中构建谓词,将来自更高级别 lambda 的"客户"值作为参数传递。

publicvoid FindInterestingOrderItems(bool onlyIncludePendingItemsOverLimit)
{
    var customers = GetCustomersWithOrders();
    var interestingOrderItems = customers
        .SelectMany(customer => customer.PendingOrders
            .SelectMany(order => order.Items
                .Where(GetFilter(customer, onlyIncludePendingItemsOverLimit))
            .Union(customer.FailedOrderItems)))
        .OrderByDescending(orderItem => orderItem.Amount);
}
private Func<OrderItem, bool> GetFilter(Customer customer, bool onlyIncludePendingItemsOverLimit)
{
    if (onlyIncludePendingItemsOverLimit)
    {
        return orderItem => orderItem.Amount > customer.MaxOrderItemAmount;
    }
    else
    {
        return orderItem => true;
    }
}