I have a table in SQL Server that has the following columns
- ID (Identity/Primary Key)
- DestinationID (Foreign key)
- Date (Date Field)
- Time (Time Field)
- DayOfWeek (Char(3) Field) i.e. “MON”,”TUE” etc..
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
Dateis nullable you could do something like this: