EF Linq中的HAVING子句

时间:2018-10-11 20:17:16

标签: c# sql sql-server entity-framework linq

我想获取客户ID列表以及该客户下的订单数量。过滤条件为:

  1. 总订单金额不超过$ 10的订单将不计算在内。
  2. 未提交至少3个订单(每个订单的总金额等于或大于10美元)的客户将不会列出。

因此,我将在SQL中执行以下操作:

SELECT customerID, COUNT(*)
FROM Orders
WHERE orderTotal > 10
GROUP BY customerID
HAVING COUNT(*) > 2

在EF中,我认为这可以表示为:

dbContext.Order
.Where(o => o.orderTotal > 10)
.GroupBy(o => o.customerID)
.Where(g => g.Count() > 2)
.ToList();

但这会产生以下SQL,该SQL使用派生表和联接,而不是简单地使用HAVING子句。我认为这在性能方面远非最佳。是否有更好的方法可以在EF中表达大小写,以便翻译后的查询将按需使用HAVING子句?

SELECT 
    [Project1].[C1] AS [C1], 
    [Project1].[customerID] AS [customerID], 
    [Project1].[C2] AS [C2], 
    [Project1].[ID] AS [ID], 

FROM ( SELECT 
    [GroupBy1].[K1] AS [customerID], 
    1 AS [C1], 
    [Extent2].[ID] AS [ID], 
    CASE WHEN ([Extent2].[storeID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
    FROM   (SELECT 
        [Extent1].[customerID] AS [K1], 
        COUNT(1) AS [A1]
        FROM [dbo].[Orders] AS [Extent1]
        WHERE [Extent1].[orderTotal] > cast(10 as decimal(18))
        GROUP BY [Extent1].[customerID] ) AS [GroupBy1]
    LEFT OUTER JOIN [dbo].[Orders] AS [Extent2] ON ([Extent2].[orderTotal] > cast(10 as decimal(18))) AND (([GroupBy1].[K1] = [Extent2].[customerID]) OR (([GroupBy1].[K1] IS NULL) AND ([Extent2].[customerID] IS NULL)))
    WHERE [GroupBy1].[A1] > 2
)  AS [Project1]
ORDER BY [Project1].[customerID] ASC, [Project1].[C2] ASC

1 个答案:

答案 0 :(得分:3)

好吧,LINQ to Entities查询不等同于SQL查询,因为它返回的分组(键和匹配元素对)列表完全没有SQL等效项。

如果仅返回customerIdCount,如SQL查询中所示:

db.Orders
.Where(o => o.orderTotal > 10)
.GroupBy(o => o.customerID)
.Select(g => new { customerId = g.Key, orderCount = g.Count() })
.Where(g => g.Count > 2)
.ToList();

然后,EF生成的SQL将与预期的几乎相同(或在功能上等同于预期):

SELECT
    [GroupBy1].[K1] AS [customerID],
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT
        [Extent1].[customerID] AS [K1],
        COUNT(1) AS [A1]
        FROM [dbo].[Orders] AS [Extent1]
        WHERE [Extent1].[orderTotal] > cast(10 as decimal(18))
        GROUP BY [Extent1].[customerID]
    )  AS [GroupBy1]
    WHERE [GroupBy1].[A1] > 2