在 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 子句,我们不能只使用简单的 Select with OrderBy 而没有 Joins 吗?谁能解释一下上面的查询?
最佳答案
底线是:因为 SQL 不能返回嵌套的结果集。
每个 SQL SELECT
语句都会返回一个简单的值列表。 LINQ 能够返回对象图,即具有嵌套对象的对象。这正是 LINQ 的 GroupBy
所做的。
在 SQL 中,GROUP BY
语句仅返回分组列和聚合结果:
SELECT StandardId, COUNT(*)
FROM Students
GROUP BY StandardId;
其余的学生栏都不见了。
LINQ GroupBy
语句返回类似的内容
StandardId
StudentId StudentName
1
21 "Student1"
15 "Student2"
2
48 "Student3"
91 "Student4"
17 "Student5"
因此,SQL GROUP BY
语句永远不能成为 LINQ GroupBy
的源。
Entity Framework (6) 知道这一点,它会生成一个 SQL 语句,从数据库中提取所有必需的数据,添加一些使分组更容易的部分,并在客户端创建分组。
关于c# - 为什么 Entity Framework linq 查询的内部 SQL 中没有 GroupBy 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62831057/