简体   繁体   English

linq-to-sql orderby 和 select null 条目

[英]linq-to-sql orderby and select null entries

I have a query that looks like this:我有一个看起来像这样的查询:

var TheOutput = from u in MyDC.TheTable
                where ....
                orderby u.TheDateTimeUsed
                select new MyModel()
                {
                   ....
                   TheDateCreated = u.TheDateTimeCreated,
                   TheDateUsed = u.TheDateTimeUsed
                   ...

                };

return TheOutput.Take(10).ToList();

u.DateTimeUsed is a nullable date. u.DateTimeUsed 是一个可为空的日期。 If the value is null, then I want these records to appear first in the list of 10 that I'm loading.如果值为 null,那么我希望这些记录首先出现在我正在加载的 10 条记录的列表中。

How can I do this?我怎样才能做到这一点?

Thanks.谢谢。

Maybe something like this:也许是这样的:

var TheOutput = from u in MyDC.TheTable
                where ....
                orderby !u.TheDateTimeUsed.HasValue
                orderby u.TheDateTimeUsed
                select new MyModel()
                {
                   ....
                   TheDateCreated = u.TheDateTimeCreated,
                   TheDateUsed = u.TheDateTimeUsed
                   ...

                };

You can also do it like this:你也可以这样做:

var TheOutput = from u in MyDC.TheTable
                where ....
                orderby u.TheDateTimeUsed??DateTime.MinValue
                select new MyModel()
                {
                   ....
                   TheDateCreated = u.TheDateTimeCreated,
                   TheDateUsed = u.TheDateTimeUsed
                   ...

                };

Not as nice as the first one.不如第一个好看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM