gpt4 book ai didi

c# - Linq order by 没有订购任何东西

转载 作者:行者123 更新时间:2023-11-30 13:13:39 26 4
gpt4 key购买 nike

// Query all records
var q =
dc.tblHelpCentreQuestions.Where(question => question.userID == UserID).Where(question => question.awaitingUserResponse == true || question.awaitingSupportResponse == true).
OrderBy(s => s.awaitingUserResponse).ThenBy(s => s.dateSubmitted).
GroupJoin(
dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question, RepliesCount = replies.Count() }
);

无论我在哪里查询排序,都好像是随机排序。

编辑:相同的结果与末尾的顺序:

// Query all records
var q =
dc.tblHelpCentreQuestions.Where(question => question.userID == UserID).Where(question => question.awaitingUserResponse == true || question.awaitingSupportResponse == true).
GroupJoin(
dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question, RepliesCount = replies.Count() }
).OrderBy(s => s.Question.awaitingUserResponse).ThenBy(s => s.Question.dateSubmitted);

编辑:生成的 SQL

exec sp_executesql N'SELECT [t2].[ID], [t2].[catID], [t2].[subject], [t2].[question], [t2].[userID], [t2].[dateSubmitted], [t2].[isUrgent], [t2].[emailMe], [t2].[awaitingSupportResponse], [t2].[awaitingUserResponse], [t2].[lastReply], [t2].[stopWatchTotalMins], [t2].[isStopWatchOn], [t2].[stopWatchStart], [t2].[priorityLevel], [t2].[value] AS [RepliesCount]
FROM (
SELECT [t0].[ID], [t0].[catID], [t0].[subject], [t0].[question], [t0].[userID], [t0].[dateSubmitted], [t0].[isUrgent], [t0].[emailMe], [t0].[awaitingSupportResponse], [t0].[awaitingUserResponse], [t0].[lastReply], [t0].[stopWatchTotalMins], [t0].[isStopWatchOn], [t0].[stopWatchStart], [t0].[priorityLevel], (
SELECT COUNT(*)
FROM [dbo].[tblHelpCentreReplies] AS [t1]
WHERE ([t0].[ID]) = [t1].[ticketID]
) AS [value]
FROM [dbo].[tblHelpCentreQuestions] AS [t0]
) AS [t2]
WHERE (([t2].[awaitingUserResponse] = @p0) OR ([t2].[awaitingSupportResponse] = @p1)) AND ([t2].[userID] = @p2)
ORDER BY [t2].[awaitingUserResponse], [t2].[dateSubmitted]', N'@p0 int,@p1 int,@p2 int', @p0 = 1, @p1 = 1, @p2 = 81

我已经通过数据库运行代码并且它正确返回了结果,所以它一定是其他奇怪的东西,因为代码非常简单,不过没关系,谢谢大家的帮助,我学到了很多新事物都存在这个问题!

最佳答案

这是 LINQ to SQL 吗?我怀疑问题是您在分组之前订购。你可以试试这个:

var q = dc.tblHelpCentreQuestions
.Where(question => question.userID == UserID)
.Where(question => question.awaitingUserResponse
|| question.awaitingSupportResponse)
.GroupJoin(dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question,
RepliesCount = replies.Count() })
.OrderBy(s => s.Question.awaitingUserResponse)
.ThenBy(s => s.Question.dateSubmitted);

编辑:好的,如果这不起作用,那么可能是 SQL 分组的限制……尽管这看起来很奇怪。

不过,您始终可以强制在客户端执行排序:

var q = dc.tblHelpCentreQuestions
.Where(question => question.userID == UserID)
.Where(question => question.awaitingUserResponse
|| question.awaitingSupportResponse)
.GroupJoin(dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new { Question = question,
RepliesCount = replies.Count() })
// Force the rest of the query to execute in .NET code (Enumerable.XXX)
.AsEnumerable()
.OrderBy(s => s.Question.awaitingUserResponse)
.ThenBy(s => s.Question.dateSubmitted);

关于c# - Linq order by 没有订购任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5541549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com