在尝试解决问题时,我注意到如果我执行LINQ to Entities
查询:
var timeRecords = db.Timesheets
.Where(timesheet => timesheet.TimesheetHeaderID == headerID);
我得到这样的查询:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[TimesheetHeaderID] AS [TimesheetHeaderID],
[Extent1].[StartDateTime] AS [StartDateTime],
[Extent1].[EndDateTime] AS [EndDateTime],
[Extent1].[ProjectCode] AS [ProjectCode],
[Extent1].[TaskCode] AS [TaskCode],
[Extent1].[WorkDescription] AS [WorkDescription]
FROM [dbo].[Timesheet] AS [Extent1]
WHERE [Extent1].[TimesheetHeaderID] = @p__linq__0
但如果我像这样添加OrderByDescending()
:
var timeRecords = db.Timesheets
.Where(timesheet => timesheet.TimesheetHeaderID == headerID)
.OrderByDescending(timesheet => timesheet.StartDateTime);
我得到一个包含如下子查询的查询:
SELECT
[Project1].[ID] AS [ID],
[Project1].[TimesheetHeaderID] AS [TimesheetHeaderID],
[Project1].[StartDateTime] AS [StartDateTime],
[Project1].[EndDateTime] AS [EndDateTime],
[Project1].[ProjectCode] AS [ProjectCode],
[Project1].[TaskCode] AS [TaskCode],
[Project1].[WorkDescription] AS [WorkDescription]
FROM ( SELECT
[Extent1].[ID] AS [ID],
[Extent1].[TimesheetHeaderID] AS [TimesheetHeaderID],
[Extent1].[StartDateTime] AS [StartDateTime],
[Extent1].[EndDateTime] AS [EndDateTime],
[Extent1].[ProjectCode] AS [ProjectCode],
[Extent1].[TaskCode] AS [TaskCode],
[Extent1].[WorkDescription] AS [WorkDescription]
FROM [dbo].[Timesheet] AS [Extent1]
WHERE [Extent1].[TimesheetHeaderID] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[StartDateTime] DESC
子查询有什么意义?
答案 0 :(得分:2)
我猜这个子查询适用于" Where" part(Where(timesheet => timesheet.TimesheetHeaderID == headerID),主查询用于OrderByDescending部分(OrderByDescending(timesheet => timesheet.StartDateTime))。 所以我假设LINQ的内部机制是分别处理每个链命令。