为什么在 Entity Framework linq 查询的内部 SQL 中没有 GroupBy 子句?
Why there is no GroupBy clause in internal SQL of Entity Framework linq query?
在 Entity Framework 的文档中:
https://www.entityframeworktutorial.net/querying-entity-graph-in-entity-framework.aspx
在有关 GroupBy 的部分中,我们可以阅读以下代码:
using (var ctx = new SchoolDBEntities())
{
var students = from s in ctx.Students
group s by s.StandardId into studentsByStandard
select studentsByStandard;
foreach (var groupItem in students)
{
Console.WriteLine(groupItem.Key);
foreach (var stud in groupItem)
{
Console.WriteLine(stud.StudentId);
}
}
}
在内部执行 SQL:
SELECT
[Project2].[C1] AS [C1],
[Project2].[StandardId] AS [StandardId],
[Project2].[C2] AS [C2],
[Project2].[StudentID] AS [StudentID],
[Project2].[StudentName] AS [StudentName],
[Project2].[StandardId1] AS [StandardId1]
FROM ( SELECT
[Distinct1].[StandardId] AS [StandardId],
1 AS [C1],
[Extent2].[StudentID] AS [StudentID],
[Extent2].[StudentName] AS [StudentName],
[Extent2].[StandardId] AS [StandardId1],
CASE WHEN ([Extent2].[StudentID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
FROM (SELECT DISTINCT
[Extent1].[StandardId] AS [StandardId]
FROM [dbo].[Student] AS [Extent1] ) AS [Distinct1]
LEFT OUTER JOIN [dbo].[Student] AS [Extent2] ON ([Distinct1].[StandardId] = [Extent2]. [StandardId]) OR (([Distinct1].[StandardId] IS NULL) AND ([Extent2].[StandardId] IS NULL))
) AS [Project2]
ORDER BY [Project2].[StandardId] ASC, [Project2].[C2] ASC
go
为什么SQL中没有GroupBy子句?如果不需要 GroupBy 子句,我们不能只使用带有 OrderBy 而没有 Joins 的简单 Select 吗?谁能解释一下上面的查询?
底线是:因为SQL不能return嵌套结果集。
每个 SQL SELECT
语句 return 都是一个简单的值列表。 LINQ 能够 returning 对象图,即具有嵌套对象的对象。这正是 LINQ 的 GroupBy
所做的。
在 SQL 中,一个 GROUP BY
语句仅 return 分组列和汇总结果:
SELECT StandardId, COUNT(*)
FROM Students
GROUP BY StandardId;
其余的学生专栏都没有了。
LINQ GroupBy
语句 return 类似于
StandardId
StudentId StudentName
1
21 "Student1"
15 "Student2"
2
48 "Student3"
91 "Student4"
17 "Student5"
因此,SQL GROUP BY
语句永远不能成为 LINQ GroupBy
.
的源
Entity Framework (6) 知道这一点并生成一个 SQL 语句,从数据库中提取所有必需的数据,添加一些使分组更容易的部分,并在客户端创建分组.
在 Entity Framework 的文档中: https://www.entityframeworktutorial.net/querying-entity-graph-in-entity-framework.aspx
在有关 GroupBy 的部分中,我们可以阅读以下代码:
using (var ctx = new SchoolDBEntities())
{
var students = from s in ctx.Students
group s by s.StandardId into studentsByStandard
select studentsByStandard;
foreach (var groupItem in students)
{
Console.WriteLine(groupItem.Key);
foreach (var stud in groupItem)
{
Console.WriteLine(stud.StudentId);
}
}
}
在内部执行 SQL:
SELECT
[Project2].[C1] AS [C1],
[Project2].[StandardId] AS [StandardId],
[Project2].[C2] AS [C2],
[Project2].[StudentID] AS [StudentID],
[Project2].[StudentName] AS [StudentName],
[Project2].[StandardId1] AS [StandardId1]
FROM ( SELECT
[Distinct1].[StandardId] AS [StandardId],
1 AS [C1],
[Extent2].[StudentID] AS [StudentID],
[Extent2].[StudentName] AS [StudentName],
[Extent2].[StandardId] AS [StandardId1],
CASE WHEN ([Extent2].[StudentID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
FROM (SELECT DISTINCT
[Extent1].[StandardId] AS [StandardId]
FROM [dbo].[Student] AS [Extent1] ) AS [Distinct1]
LEFT OUTER JOIN [dbo].[Student] AS [Extent2] ON ([Distinct1].[StandardId] = [Extent2]. [StandardId]) OR (([Distinct1].[StandardId] IS NULL) AND ([Extent2].[StandardId] IS NULL))
) AS [Project2]
ORDER BY [Project2].[StandardId] ASC, [Project2].[C2] ASC
go
为什么SQL中没有GroupBy子句?如果不需要 GroupBy 子句,我们不能只使用带有 OrderBy 而没有 Joins 的简单 Select 吗?谁能解释一下上面的查询?
底线是:因为SQL不能return嵌套结果集。
每个 SQL SELECT
语句 return 都是一个简单的值列表。 LINQ 能够 returning 对象图,即具有嵌套对象的对象。这正是 LINQ 的 GroupBy
所做的。
在 SQL 中,一个 GROUP BY
语句仅 return 分组列和汇总结果:
SELECT StandardId, COUNT(*)
FROM Students
GROUP BY StandardId;
其余的学生专栏都没有了。
LINQ GroupBy
语句 return 类似于
StandardId
StudentId StudentName
1
21 "Student1"
15 "Student2"
2
48 "Student3"
91 "Student4"
17 "Student5"
因此,SQL GROUP BY
语句永远不能成为 LINQ GroupBy
.
Entity Framework (6) 知道这一点并生成一个 SQL 语句,从数据库中提取所有必需的数据,添加一些使分组更容易的部分,并在客户端创建分组.