将 WeeklyTrigger.DaysOfWeek 转换为 (DayOfWeek) 的列表

Convert WeeklyTrigger.DaysOfWeek to List of (DayOfWeek)

我在看https://msdn.microsoft.com/en-us/library/windows/desktop/aa384024%28v=vs.85%29.aspx

Sunday  = 1
Monday  = 2
Tuesday = 4
Wednesday =8
Thursday = 16
Friday = 32
Saturday= 64

此 属性 包含计划任务中所选 DayOfWeek 的聚合值。例如,值 66 是星期一或星期日。

我想将此值转换为一个列表(属于 DayOfWeek)。

像这样转换值的最佳方法是什么?

var weekDays = (int)daysOfWeek;
var weekDayEnumerable = Enumerable
    .Range(0, 7)
    .Where(i => { var isMatch = (weekDays & 1) == 1; weekDays >>= 1; return isMatch; })
    .Select(i => (DayOfWeek)i)
    .ToList();

好吧,也许太过努力避免循环,但如果您愿意,也可以转换为类似的 for 循环。

更新:

不做任何改变的版本:

var weekDays = (int)daysOfWeek;
var weekDayEnumerable = Enumerable
    .Range(0, 7)
    .Where(i => (weekDays & (1 << i)) > 0)
    .Select(i => (DayOfWeek)i)
    .ToList();