我对实体框架ORDER BY
上不受欢迎的GroupJoin
有疑问。
但首先是我的查询。它是一个带有JOIN和ORDER的简单查询链。
var dataQuery = model.InverterData
.Where(d => d.Timestamp >= from && d.Timestamp < till)
.Select(d => new
{
d.InverterID,
d.Timestamp
});
var dayDataQuery = model.InverterDayData
.GroupJoin(dataQuery
, outer => outer.InverterID
, inner => inner.InverterID
, (outer, inner) => new
{
outer.InverterID,
outer.Date,
outer.DayYield,
InverterData = inner.Select(d => new
{
d.Timestamp
})
});
var orderedQuery = dayDataQuery
.OrderBy(d => d.DayYield)
.ThenBy(d => d.InverterID);
我现在的问题是GroupJoin在最后建立了一个带有不需要的ORDER BY的查询。这使我的命令毫无意义。
但详情......
SELECT
1 AS [C1],
[Extent1].[InverterID] AS [InverterID],
[Extent1].[Timestamp] AS [Timestamp]
FROM [data].[InverterData] AS [Extent1]
WHERE ([Extent1].[Timestamp] >= @p__linq__0) AND ([Extent1].[Timestamp] < @p__linq__1)
看起来不错!
SELECT
[Project1].[C1] AS [C1],
[Project1].[InverterID] AS [InverterID],
[Project1].[Date] AS [Date],
[Project1].[DayYield] AS [DayYield],
[Project1].[C2] AS [C2],
[Project1].[Timestamp] AS [Timestamp]
FROM ( SELECT
[Extent1].[InverterID] AS [InverterID],
[Extent1].[Date] AS [Date],
[Extent1].[DayYield] AS [DayYield],
1 AS [C1],
[Extent2].[Timestamp] AS [Timestamp],
CASE WHEN ([Extent2].[InverterID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
FROM [data].[InverterDayData] AS [Extent1]
LEFT OUTER JOIN [data].[InverterData] AS [Extent2] ON ([Extent2].[Timestamp] >= @p__linq__0) AND ([Extent2].[Timestamp] < @p__linq__1) AND ([Extent1].[InverterID] = [Extent2].[InverterID])
) AS [Project1]
ORDER BY [Project1].[InverterID] ASC, [Project1].[Date] ASC, [Project1].[C2] ASC
添加了不需要的ORDER BY ORDER BY [Project1].[InverterID] ASC, [Project1].[Date] ASC, [Project1].[C2] ASC
!
SELECT
[Project1].[InverterID] AS [InverterID],
[Project1].[C1] AS [C1],
[Project1].[Date] AS [Date],
[Project1].[DayYield] AS [DayYield],
[Project1].[C2] AS [C2],
[Project1].[Timestamp] AS [Timestamp]
FROM ( SELECT
[Extent1].[InverterID] AS [InverterID],
[Extent1].[Date] AS [Date],
[Extent1].[DayYield] AS [DayYield],
1 AS [C1],
[Extent2].[Timestamp] AS [Timestamp],
CASE WHEN ([Extent2].[InverterID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
FROM [data].[InverterDayData] AS [Extent1]
LEFT OUTER JOIN [data].[InverterData] AS [Extent2] ON ([Extent2].[Timestamp] >= @p__linq__0) AND ([Extent2].[Timestamp] < @p__linq__1) AND ([Extent1].[InverterID] = [Extent2].[InverterID])
) AS [Project1]
ORDER BY [Project1].[DayYield] ASC, [Project1].[InverterID] ASC, [Project1].[Date] ASC, [Project1].[C2] ASC
我的订单(由DayYield
和InverterID
)无效,因为Date
和C2
现在也包括在内。
为什么和/或如何使其正常工作? 需要服务器上订单,因为服务器上的分页。