按日期排序列表,将未来分为过去日期

时间:2014-08-02 14:37:26

标签: c# linq entity-framework

我使用Linq to Entities从数据库获取记录如下:

context.Orders.OrderBy(x => x.Schedule)

在这种情况下,我按DateTime Schedule ...

订购

我仍然希望按日期时间表排序,但首先我想展示那些日期是在今天广告之后的那些日期之后的日期。

我该怎么做?

2 个答案:

答案 0 :(得分:7)

是的,听起来像你想要的东西:

DateTime startOfDay = DateTime.UtcNow.Date;
var results = context.Orders
                     .OrderBy(x => x.Schedule < startOfDay)
                     .ThenBy(x => x.Schedule);

假设EF仍然订购&#34; false&#34;之前&#34; true&#34; - 当然,它可以在LINQ to Objects中使用,但我不确定在EF中。值得一试。

答案 1 :(得分:3)

你可以试试这个:

 var AfterDates = context.Orders.OrderBy(x => x.Schedule).Where(y=> y.Schedule > DateTime.Now).ToList();
 var BeforeDates = context.Orders.OrderBy(x => x.Schedule).Where(y=> y.Schedule < DateTime.Now).ToList();
 // You can make another query for the Same Dates (excluded from my solution)
 // You can join these two lists if necessary 

现在你有两个有序列表,一个用于今天之后的日期,另一个用于之前的日期。