.OrderBy(DayOfWeek)将星期日视为一周的结束 [英] .OrderBy(DayOfWeek) to treat Sunday as the end of the week
问题描述
我正在通过其 System.DayOfWeek
属性订购许多对象.
I'm ordering a number of objects by their System.DayOfWeek
property.
DayOfWeek 将星期日视为该广告的开始周,而我希望订购它,以便它出现在 end 处.这只是一个枚举,所以我无法对其进行修改.但是,我读到我可以创建自定义文化,但认为这可能是过分的.
DayOfWeek treats Sunday as the start of the week, whereas I would like it to be ordered so it appears at the end. It's just an enum, so I can't modify it. However I've read that I may be able to create a custom culture but think this is probably overkill.
List<TimeBand> orderedTimeBands = timeBands.OrderBy(x => x.DayName).ToList()
所以DayName
是DayOfWeek
,我希望从星期一->星期日开始订购orderedTimeBands
.
So DayName
is a DayOfWeek
, i want orderedTimeBands
to be ordered from Monday -> Sunday.
有什么想法吗?
推荐答案
最简单的方法是:
var orderedTimeBands = timeBands.OrderBy(x => ((int) x.DayOfWeek + 6) % 7)
.ToList()
所以我们有:
Name Original value Value after arithmetic
Sunday 0 6
Monday 1 0
Tuesday 2 1
Wednesday 3 2
Thursday 4 3
Friday 5 4
Saturday 6 5
...我想这就是你想要的.
... which is what you want, I think.
这篇关于.OrderBy(DayOfWeek)将星期日视为一周的结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!