我试图将linq分解为sql查询以使它们更具可读性.
假设我想要返回上一年有超过100个订单的产品的所有订单.我有这个问题:
from o in _context.Orders
where (from o1 in _context.Orders
where o1.Year == o.Year - 1 && o1.Product == o.Product
select o1).Count() > 100
select o;
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是将嵌套查询放在一个可重用的函数中:
private IQueryable<Order> LastSeasonOrders(Order order)
{
return (from o in _context.Orders
where o.Year == order.Year - 1 && o.Product == order.Product
select o);
}
Run Code Online (Sandbox Code Playgroud)
然后让我将原始查询更改为:
from o in _context.Orders
where LastSeasonOrders(o).Count() > 100
select o;
Run Code Online (Sandbox Code Playgroud)
但这不起作用,但有一个例外,即在运行查询时无法将方法调用转换为SQL.
有关正确方法的任何快速提示吗?