Exclude one value in LINQ OrderBY
.net, c#, linq, sql-order-by
Solution
You got an impression of numerical sort because of condition you used, precisely `a => a.DayOfWeek != 1`. It will take all non-Sundays (`true`) and place them after Sundays (`false`), which is how `OrderBy` sorts `boolean` values.
Try either:
- `.OrderByDescending(a => a.DayofWeek != 1).ThenBy(a => a.DayofWeek)`
- `.OrderBy(a => a.DayofWeek == 1).ThenBy(a => a.DayofWeek)`
Problem
I have a list, and I would like it to be sorted by DayOfWeek, except for the case where DayOfWeek=1, which should go at the end of the list. I have tried a few variations on OrderBy and ThenBy, but it seems to continue to sort in numerical order. I am sure this is very simple, and I don't want to change the value of the Day=1, which was the original solution ``` var dayOfWeeksHolder = Enumerable.Range(1, 7); var defaultTime = DateTime.Today.TimeOfDay; var psList = _pickingScheduleIO.GetDayScheduleList(branchNo); var psListByType = psList.Where(p => p.PickingSesstionTypeId == pickingSessionTypeId).ToList(); var fullList = (from d in dayOfWeeksHolder join s in psListByType on d equals s.DayOfWeek into grp from ds in grp.DefaultIfEmpty() // where (ds == null || ds.PickingSesstionTypeId == pickingSessionTypeId) select new DayScheduleVM { PickingScheduleId = ds == null ? 0 : ds.PickingSessionScheduleId, DayOfWeek = d, //d == 1 ? d + 7 : d, //in sql sunday is 1, we need to make it to 8 so it will be sorted to last StartTime = ds == null ? defaultTime : ds.StartTime, EndTime = ds == null ? defaultTime : ds.EndTime, PickingSessionTypeId = pickingSessionTypeId }).OrderBy(a => a.DayOfWeek !=1).ThenBy(a => a.DayOfWeek); return fullList.ToList(); ```