根据双重嵌套实体的属性对主要实体进行排序
我将 Entity Framework Core 与 ASP.Net MVC 结合使用。我的业务对象模型部分由作业(主要实体)组成,每个作业都包含一个或多个项目,每个项目都有零个或多个计划(链接到一组部门,但这在这里并不重要)。每个计划都有一个 StartDate 和 EndDate。
这是一个(简化的)类图(它反映了您所期望的数据库架构):
我想按 Schedule 实体中最早的 StartDateTime 值对作业列表进行排序。我还没能想出一个 LINQ 链来完成这个任务。目前,我已经通过直接在控制器中使用 ADO.Net 来根据以下 SQL 语句组装作业列表来构建我想要的功能:
@"SELECT
Jobs.JobId,
Jobs.JobName,
Jobs.jobNumber,
MIN(ProjectSchedules.StartDateTime) AS StartDateTime
FROM
Jobs INNER JOIN Projects ON Jobs.JobID = Projects.JobID
LEFT JOIN ProjectSchedules ON Projects.ProjectID = ProjectSchedules.ProjectID
GROUP BY Jobs.JobId, Jobs.JobName, Jobs.JobNumber
ORDER BY StartDateTime"
我更愿意正确使用 EF Core,而不是进行最终运行围绕它。生成此 SQL 语句(或等效语句)的 LINQ 语句是什么?
I'm using Entity Framework Core with ASP.Net MVC. My business object model consists, in part, of Jobs (the primary entities), each of which contains one or more Projects, and each Project has zero or more Schedules (which link to sets of Departments, but that's not important here). Each schedule has a StartDate and and EndDate.
Here is a (simplified) class diagram (which reflects the database schema as you would expect):
I want to sort the Jobs list by the earliest StartDateTime value in the Schedule entity. I haven't been able to come up with a LINQ chain that accomplishes this. For the time being, I have cobbed the functionality I want by using ADO.Net directly in my controller to assemble the Jobs list based on the following SQL Statement:
@"SELECT
Jobs.JobId,
Jobs.JobName,
Jobs.jobNumber,
MIN(ProjectSchedules.StartDateTime) AS StartDateTime
FROM
Jobs INNER JOIN Projects ON Jobs.JobID = Projects.JobID
LEFT JOIN ProjectSchedules ON Projects.ProjectID = ProjectSchedules.ProjectID
GROUP BY Jobs.JobId, Jobs.JobName, Jobs.JobNumber
ORDER BY StartDateTime"
I would prefer to use EF Core properly, rather than to do an end-run around it. What would be the LINQ statements to generate this SQL statement (or an equivalent one)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个查询来“收集”每个作业的项目计划中的所有
StartDateTime
,获取其最低值,然后按该值排序:如您所见,甚至没有
Min
在那里运行。该函数可以使用,但它的性能可能会更差,因为它必须评估所有StartDate
值,而排序可以使用有序索引。无论哪种方式,为了进行比较,这是使用
Min()
:You need a query that "collects" all
StartDateTime
s in schedules of projects per job, takes their lowest value then sorts by that value:As you see, there's not even a
Min
function in there. The function could be used, but it may perform worse, because it has to evaluate allStartDate
values, while the ordering could make use of an ordered index.Either way, for comparison, this is with
Min()
:首先,我们需要检索所有可以使用 Include 语句执行此操作的实体,然后使用 theninclude 进一步检索实体。
dbcontext.Jobs.Include(j => j.Projects).ThenInclude(p => p.Schedules)
现在我们拥有了所有实体,您可以进行所有排序、分组或其他操作你想做的事。
对我来说,听起来您想在
schedule.startdatetime
上Orderby
。First we need to retrieve all the entities we can do this with the Include statement, and we use theninclude to retrieve entities one further down.
dbcontext.Jobs.Include(j => j.Projects).ThenInclude(p => p.Schedules)
Now that we have all the entities you can do all the sorting, grouping or whatever else you wish to do.
To me it sounds like you want to
Orderby
onschedule.startdatetime
.