I have a table in SQL Server that has the following columns
I want to order the data by Date and Time, then by DayOfWeek and Time. So all the records with dates appear at the top, and ordered by time, then the DayOfWeek order by time.
I have tried several ways but just cant get my head around it, I have the following code
var qry = ctx.DestinationTimings.Where(x => x.DestinationID == this.ID)
.OrderBy(t => t.Date).ThenBy(t => t.Date != null)
.ThenBy(t => SqlFunctions.CharIndex(t.DayOfWeek + ",", "MON,TUE,WED,THU,FRI,SAT,SUN"))
.ThenBy(t => t.Time);
If Date
is nullable you could do something like this:
var qry = ctx.DestinationTimings.Where(x => x.DestinationID == this.ID)
.OrderBy(t => t.Date ?? DateTime.MaxValue)
.ThenBy(t => SqlFunctions.CharIndex(t.DayOfWeek + ",", "MON,TUE,WED,THU,FRI,SAT,SUN"))
.ThenBy(t => t.Time);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With