How can I get the Linq-to-Entities provider to truly perform a GROUP BY? No matter what I do, it always generates SQL that is far slower than an actual GROUP BY. For example:
var foo = (from x in Context.AccountQuantities
  where x.AccountID == 27777
  group x by x.StartDate
  into groups
  select new
    {
      groups.FirstOrDefault().StartDate,
      Quantity = groups.Max(y => y.Quantity)
    }).ToList();
translates to:
SELECT 
1 AS [C1], 
[Project4].[C1] AS [C2], 
[Project4].[C2] AS [C3]
FROM ( SELECT 
    [Project3].[C1] AS [C1], 
    (SELECT 
        MAX([Extent3].[Quantity]) AS [A1]
        FROM [dbo].[AccountQuantities] AS [Extent3]
        WHERE (27777 = [Extent3].[AccountID]) AND ([Project3].[StartDate] = [Extent3].[StartDate])) AS [C2]
    FROM ( SELECT 
        [Distinct1].[StartDate] AS [StartDate], 
        (SELECT TOP (1) 
            [Extent2].[StartDate] AS [StartDate]
            FROM [dbo].[AccountQuantities] AS [Extent2]
            WHERE (27777 = [Extent2].[AccountID]) AND ([Distinct1].[StartDate] = [Extent2].[StartDate])) AS [C1]
        FROM ( SELECT DISTINCT 
            [Extent1].[StartDate] AS [StartDate]
            FROM [dbo].[AccountQuantities] AS [Extent1]
            WHERE 27777 = [Extent1].[AccountID]
        )  AS [Distinct1]
    )  AS [Project3]
)  AS [Project4]
How can I get it to execute this instead?
SELECT
AccountQuantities.StartDate,
MAX(AccountQuantities.Quantity)
FROM AccountQuantities
WHERE AccountID=27777
GROUP BY StartDate
Again, note the lack of ANY GROUP BY in what's executed. I'm fine with EF optimi开发者_JAVA技巧zing things non-ideally but this is orders of magnitude slower, I cannot find any way to convince it to really do a GROUP BY, and is a major problem for us!
Use groups.Key instead of groups.FirstOrDefault().StartDate
var foo = (from x in Context.AccountQuantities
           where x.AccountID == 27777
           group x by x.StartDate
           into groups
           select new
           {
             groups.Key,
             Quantity = groups.Max(y => y.Quantity)
            }).ToList();
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论