从实体框架获取最大值和最小值,在一个查询中,并尽可能使用最佳查询

本文关键字:查询 一个 最佳 尽可能 获取 框架 实体 最大值 最小值 | 更新日期: 2023-09-27 18:33:50

我知道这个问题,但我想做的是获得接近这个生成的SQL的东西:

select MAX(Column), MIN(Column) from Table WHERE Id = 1

当我尝试这个时:

var query = from d in db.Table
            where d.Id == 1
            select new
            {
                min = db.Table.Max(s => s.Column),
                max = db.Table.Min(s => s.Column)
            };

生成的 sql 如下所示:

SELECT 
    [Extent1].[Id] AS [Id], 
    [GroupBy1].[A1] AS [C1], 
    [GroupBy2].[A1] AS [C2]
    FROM   [dbo].[Table] AS [Extent1]
    CROSS JOIN  (SELECT 
        MAX([Extent2].[Column]) AS [A1]
        FROM [dbo].[Table] AS [Extent2] ) AS [GroupBy1]
    CROSS JOIN  (SELECT 
        MIN([Extent3].[Column]) AS [A1]
        FROM [dbo].[Table] AS [Extent3] ) AS [GroupBy2]
    WHERE ([Extent1].[Id] = 1) AND (1 IS NOT NULL)

我也试过这个:

var query = from d in db.Table
           where d.Id == 1
           group d by d.Id into grp
           let min = grp.Min(s => s.Column)
           let max = grp.Max(s => s.Column)
           select new { min, max };

这给出了这个:

SELECT 
    [Project2].[Id] AS [Id], 
    [Project2].[C1] AS [C1], 
    [Project2].[C2] AS [C2]
    FROM ( SELECT 
        [Project1].[C1] AS [C1], 
        [Project1].[Id] AS [Id], 
        (SELECT 
            MAX([Extent2].[Column]) AS [A1]
            FROM [dbo].[Table] AS [Extent2]
            WHERE ([Extent2].[Id] = 1) AND (1 IS NOT NULL) AND ([Project1].[Id] = [Extent2].[Id])) AS [C2]
        FROM ( SELECT 
            [GroupBy1].[A1] AS [C1], 
            [GroupBy1].[K1] AS [Id]
            FROM ( SELECT 
                [Extent1].[Id] AS [K1], 
                MIN([Extent1].[Column]) AS [A1]
                FROM [dbo].[Table] AS [Extent1]
                WHERE ([Extent1].[Id] = 16) AND (16 IS NOT NULL)
                GROUP BY [Extent1].[Id]
            )  AS [GroupBy1]
        )  AS [Project1]
    )  AS [Project2]

它们都有效,性能影响可能可以忽略不计,所以主要是美学:
这两个生成的询问都伤害了我的眼睛。

从实体框架获取最大值和最小值,在一个查询中,并尽可能使用最佳查询

尝试删除 let 语句 - 下面会产生预期的结果:

var q = from d in db.Table
        where d.Id == 1
        group d by d.Id into g
        select new
        {
            Id = g.Key, // shown for illustrative purposes
            ColumnMin = g.Min( gi => gi.Column ),
            ColumnMax = g.Max( gi => gi.Column )
        };
var result = q.SingleOrDefault();

生成的 SQL:

SELECT 
    [GroupBy1].[K1] AS [Id],
    [GroupBy1].[A1] AS [C1], 
    [GroupBy1].[A2] AS [C2]
    FROM ( SELECT 
        [Extent1].[Id] AS [K1], 
        MIN([Extent1].[Column]) AS [A1], 
        MAX([Extent1].[Column]) AS [A2]
        FROM [dbo].[Table] AS [Extent1]
        WHERE 1 = [Extent1].[Id]
        GROUP BY [Extent1].[Id]
    )  AS [GroupBy1]

8年后,我们来了!但还是有答案!

db.Table
  .Where(d => d.Id == 1)
  .GroupBy(_ => 1,
    (_, records) => new
                    {
                        Max = records.Max(r => r.Column),
                        Min = records.Min(r => r.Column)
                     });

这导致:

SELECT MAX([o].[Column]) AS [Max], MIN([o].[Column]) AS [Min]
FROM [Table] AS [o]
WHERE [o].[Id] = 1
GO

请注意,在"分组依据 1"中,1 与查询中的 ID 无关!

var query = from d in db.Table
            where d.Id == 1
            select
            {     
                d.Max(t =>t.yourColName),  
                d.Min(t =>t.yourColName)  
            };