过去,我通过向 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<>
(or Expression<Func<>>
如果使用 LinqToEntities),如下所示:
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 移动到 a Func<>
,因为它包含对customer
由查询的更高级别部分定义的变量 ( ) 的引用。我在这里想念什么?