Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order list by Date with split between future as past dates

I am getting records from a database using Linq to Entities as follows:

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

In this case I am ordering by DateTime Schedule ...

I would like to still order by DateTime Schedule but first I would like to show the ones which date is after today ad after it the ones which day are in the past.

How can I do this?

like image 464
Miguel Moura Avatar asked Feb 06 '26 17:02

Miguel Moura


1 Answers

Right, it sounds like you want something like:

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

That's assuming that EF still orders "false" before "true" - it would work in LINQ to Objects, certainly, but I don't know for sure in EF. Worth a try.

like image 93
Jon Skeet Avatar answered Feb 09 '26 06:02

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!