gpt4 book ai didi

c# - 在 Linq to SQL 中订购可为 null 的 DateTime

转载 作者:IT王子 更新时间:2023-10-29 04:26:19 25 4
gpt4 key购买 nike

我已经开始将 Linq to SQL 用于我正在进行的项目,并且在按 DateTime 字段排序时遇到了问题,但是由于 DateTime 允许空值,因此出现的空值小于实际日期。

所以我非常希望有日期的排在最前面(无论哪种方式排序),然后所有没有设置日期的排在最前面。

jobList = from ju in context.Job_Users_Assigned
where ju.UserID == user.ID
select ju.Job;
return jobList.OrderByDescending(j => j.EndDate);

最佳答案

这有点 hack,但它似乎适用于 Linq to SQL:

return from ju in context.Job_Users_Assigned
where ju.UserID == user.ID
orderby ju.Created ?? DateTime.MaxValue descending;

因此,当实际“Create”值为空时,我将替换最大可能的 DateTime 值。这会将所有空值放在顶部。

另一种方法是根据日期字段是否有值来排序。这也适用:

return from ju in context.Job_Users_Assigned
where ju.UserID == user.ID
orderby ju.Created.HasValue descending
orderby ju.Created descending;

关于c# - 在 Linq to SQL 中订购可为 null 的 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1107767/

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