订购日期时间为空的LINQ to SQL中 [英] Ordering nullable DateTime in Linq to SQL

查看:114
本文介绍了订购日期时间为空的LINQ to SQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用LINQ to SQL中的一个项目IM工作的开始,我有一个DateTime字段订货时碰到一个问题,但是自从日期时间允许空值的空都上来了小于实际的日期在那里。



所以我非常想约会的那些是在顶部(订购两种方式),那么所有没有设定日期的人。

 招贤纳才从菊=在context.Job_Users_Assigned 
,其中ju.UserID == user.ID
选择ju.Job;
返回jobList.OrderByDescending(J => j.EndDate);


解决方案

这是一个黑客位,但它似乎使用LINQ合作,SQL:从菊

 收益在context.Job_Users_Assigned 
,其中ju.UserID ==用户。 ID
排序依据ju.Created? DateTime.MaxValue下降;



所以,当实际的创建值为null我代最大可能的日期时间值。这将会把所有的空值在顶部。



另一种方法是日期字段是否具有价值订购。这工作太:

 从菊context.Job_Users_Assigned 
,其中ju.UserID == user.ID $ b返回$ b排序依据ju.Created.HasValue降
排序依据ju.Created下降;


I have started using Linq to SQL for a project im working on and i have run into a problem when ordering by a DateTime field but since the DateTime allows nulls the nulls are coming up as less than the actual dates in there.

So i pretty much want the ones with a date to be at the top (ordered either way) then all the ones with no date set.

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

解决方案

This is a bit of a hack, but it appears to work with Linq to SQL:

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

So I'm substituting the maximum possible DateTime value when the actual "Create" value is null. That'll put all the null values at the top.

Another approach is to order by whether the date field has a value. This works too:

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

这篇关于订购日期时间为空的LINQ to SQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆