LINQ:分组依据和任何

LINQ: Group by and Any

当我将此 LINQ 查询发送到我的数据库时

var temp = dbContext.CustomerOrders
                        .Where(myOrder => myOrder.CustomerID == CustomerID
                                && myOrder.CustomerOrderLines.Any(ol => ol.Article != null && ol.Article.ArticleCode.Contains(TheValue)))                            
                        .OrderByDescending(myOrder => myOrder.ID)
                        .GroupBy(myOrder => myOrder.ID
                        , (g) => new CustomerControlOrderInfo()
                        {
                            CustomerOrderID = g.ID,
                            OrderDate = g.OrderDate,
                            NrOffArticles = string.Format("{0:N0}", g.CustomerOrderLines.Sum(ol => ol.Amount).ToString()),
                            AmountBeforeVAT = string.Format("{0:C}", g.CustomerOrderLines.Sum(o => o.TotalPriceExcl * o.Amount)),
                            //AmountAfterVAT = string.Format("{0:C2}", g.CustomerOrderLines.Sum(o => o.TotalPriceIncl * o.Amount).ToString()),
                            InvoiceNr = g.CustomerInvoices.Any() ? g.CustomerInvoices.LastOrDefault().InvoiceNumber : string.Empty,
                            Agent = g.Agent.Name,
                            Credit = g.CreditOrder,
                            Consignment = g.Consignment,
                            Approved = g.Approved,
                            Invoiced = g.CustomerOrderLines.Count(ol => ol.Invoiced == true) > 0 ? true : false,
                            Send = g.CustomerOrderLines.Count(ol => ol.SendDate != null) > 0 ? true : false
                        })
                        .ToList();

它会导致错误 "subquery returns more than row"。如果 CustomerOrderline 超过 CustomerOrderLine,就会发生这种情况。

当我查看生成的 SQL 时,似乎缺少一个 GROUP By。 编辑:,SQL 添加

SELECT a.[i_d] AS COL1, a.[i_d] AS COL2, a.[OrderDate] AS COL3
,  (    SELECT 1
    FROM [CustomerOrderLine] f 
    WHERE a.[i_d] = f.[CustomerOrderID])  AS  EXPR4
, c.[Name] AS COL5
,  (CASE WHEN a.[CreditOrder] <> 0 THEN 1 ELSE 0 END)  AS  EXPR6
,  (CASE WHEN a.[Consignment] <> 0 THEN 1 ELSE 0 END)  AS  EXPR7
,  (CASE WHEN a.[Approved] <> 0 THEN 1 ELSE 0 END)  AS  EXPR8
,  (    SELECT 1                  
    FROM [CustomerOrderLine] g 
    WHERE a.[i_d] = g.[CustomerOrderID])  AS  EXPR9
,  (    SELECT 1                  
    FROM [CustomerOrderLine] h 
    WHERE a.[i_d] = h.[CustomerOrderID])  AS  EXPR10 
FROM [CustomerOrder] a 
    LEFT JOIN [Agent] AS b ON (a.[AgentID] = b.[i_d]) 
    LEFT JOIN [Person] AS c ON (b.[i_d] = c.[i_d]) 
WHERE a.[i_d] = 7085                                       
AND a.[CustomerID] = 185 
AND EXISTS 
(   SELECT 1                  
    FROM [CustomerOrderLine] d 
    JOIN [Article] AS e ON (d.[ArticleID] = e.[i_d]) 
WHERE a.[i_d] = d.[CustomerOrderID] 
AND d.[ArticleID] IS NOT NULL 
AND e.[ArticleCode] LIKE '%mgpn%') 
ORDER BY COL1 DESC

我需要更改什么才能更正此问题?

我在 LINQ 中使用 .All 进行了尝试:

var temp = dbContext.CustomerOrders
                        .Where(myOrder => myOrder.CustomerID == CustomerID
                                && myOrder.CustomerOrderLines.All(ol => ol.Article != null && ol.Article.ArticleCode.Contains(TheValue)))                            
                        .OrderByDescending(myOrder => myOrder.ID)
                        .GroupBy(myOrder => myOrder.ID
                        , (g) => new CustomerControlOrderInfo()
                        {
                            CustomerOrderID = g.ID,
                            OrderDate = g.OrderDate,
                            NrOffArticles = string.Format("{0:N0}", g.CustomerOrderLines.Sum(ol => ol.Amount).ToString()),
                            AmountBeforeVAT = string.Format("{0:C}", g.CustomerOrderLines.Sum(o => o.TotalPriceExcl * o.Amount)),
                            //AmountAfterVAT = string.Format("{0:C2}", g.CustomerOrderLines.Sum(o => o.TotalPriceIncl * o.Amount).ToString()),
                            InvoiceNr = g.CustomerInvoices.Any() ? g.CustomerInvoices.LastOrDefault().InvoiceNumber : string.Empty,
                            Agent = g.Agent.Name,
                            Credit = g.CreditOrder,
                            Consignment = g.Consignment,
                            Approved = g.Approved,
                            Invoiced = g.CustomerOrderLines.Count(ol => ol.Invoiced == true) > 0 ? true : false,
                            Send = g.CustomerOrderLines.Count(ol => ol.SendDate != null) > 0 ? true : false
                        })
                        .ToList();

结果是正确的 SQL 但不是所有的订单都被找到(遗漏的是超过一行的订单):

SELECT a.[i_d] AS COL1, a.[i_d] AS COL2, a.[OrderDate] AS COL3
,  (    SELECT 1                  
    FROM [CustomerOrderLine] f 
    WHERE a.[i_d] = f.[CustomerOrderID])  AS  EXPR4
, c.[Name] AS COL5
,  (CASE WHEN a.[CreditOrder] <> 0 THEN 1 ELSE 0 END)  AS  EXPR6
,  (CASE WHEN a.[Consignment] <> 0 THEN 1 ELSE 0 END)  AS  EXPR7
,  (CASE WHEN a.[Approved] <> 0 THEN 1 ELSE 0 END)  AS  EXPR8
,  (    SELECT 1                  
    FROM [CustomerOrderLine] g 
    WHERE a.[i_d] = g.[CustomerOrderID])  AS  EXPR9
,  (    SELECT 1                  
    FROM [CustomerOrderLine] h 
    WHERE a.[i_d] = h.[CustomerOrderID])  AS  EXPR10 
FROM [CustomerOrder] a 
    LEFT JOIN [Agent] AS b ON (a.[AgentID] = b.[i_d]) 
    LEFT JOIN [Person] AS c ON (b.[i_d] = c.[i_d]) 
WHERE a.[i_d] = 7085
AND a.[CustomerID] = 185
AND  
(   SELECT  (CASE WHEN COUNT(*) = 0 THEN 1 ELSE 0 END)                   
FROM [CustomerOrderLine] d 
WHERE a.[i_d] = d.[CustomerOrderID] 
AND not (
            EXISTS 
            (   SELECT 1234567 
                FROM [Article] e 
                WHERE d.[ArticleID] = e.[i_d] 
                AND d.[ArticleID] IS NOT NULL 
                AND e.[ArticleCode] LIKE '%mgpn%'
            )
        )
)  <> 0 ORDER BY COL1 DESC

杰伦

jdweng 让我走上正轨,调整了 LINQ:

var temp = dbContext.CustomerOrders
                        .Where(myOrder => myOrder.CustomerID == CustomerID
                                && myOrder.CustomerOrderLines.Any(ol => ol.Article != null && ol.Article.ArticleCode.Contains(TheValue)))                            
                        .OrderByDescending(myOrder => myOrder.ID).ToList()
                        .GroupBy(myOrder => myOrder.ID
                        , (g) => new CustomerControlOrderInfo()
                        {
                            CustomerOrderID = g.ID,
                            OrderDate = g.OrderDate,
                            NrOffArticles = string.Format("{0:N0}", g.CustomerOrderLines.Sum(ol => ol.Amount).ToString()),
                            AmountBeforeVAT = string.Format("{0:C}", g.CustomerOrderLines.Sum(o => o.TotalPriceExcl * o.Amount)),
                            //AmountAfterVAT = string.Format("{0:C2}", g.CustomerOrderLines.Sum(o => o.TotalPriceIncl * o.Amount).ToString()),
                            InvoiceNr = g.CustomerInvoices.Any() ? g.CustomerInvoices.LastOrDefault().InvoiceNumber : string.Empty,
                            Agent = g.Agent.Name,
                            Credit = g.CreditOrder,
                            Consignment = g.Consignment,
                            Approved = g.Approved,
                            Invoiced = g.CustomerOrderLines.Count(ol => ol.Invoiced == true) > 0 ? true : false,
                            Send = g.CustomerOrderLines.Count(ol => ol.SendDate != null) > 0 ? true : false
                        })
                        .ToList();

结果如下 SQL:

SELECT a.[i_d] AS COL1
FROM [CustomerOrder] a 
WHERE a.[CustomerID] = 185
AND EXISTS 
(   SELECT 1                  
FROM [CustomerOrderLine] b 
    JOIN [Article] AS c ON (b.[ArticleID] = c.[i_d]) 
WHERE a.[i_d] = b.[CustomerOrderID] 
AND b.[ArticleID] IS NOT NULL 
AND c.[ArticleCode] LIKE '%mgpn%') 
ORDER BY COL1 DESC

这避免了对 GROUP BY 的需要,并生成完整列表。

杰伦