我有一个列表,我希望它按 DayOfWeek 排序,除了 DayOfWeek=1 的情况,它应该放在列表的末尾。我在 OrderBy 和 ThenBy 上尝试了一些变体,但它似乎继续按数字顺序排序。
我确信这很简单,我不想更改 Day=1 的值,这是原来的解决方案
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();