我有一个员工班次的天数(工作日)列表如下:
List<int> workingDays = new List<int>();
workingDays.Add(1); // monday
workingDays.Add(2); // tuesday
workingDays.Add(3); // wednesday
workingDays.Add(5); // friday
这是员工的工作日。
现在,我需要在一个非工作日的选定月份中获得所有日子。
例如:
如果7月有31天,我应该得到以下列表:
July 3 // thursday is a non-working day
July 5 // saturday is a non-working day
July 6 // sunday is a nono-working day
July 10 // thursday is a non-working day
等等......
有关如何获取该列表的任何线索?
更新:
这是我的尝试:
for (int day = 1; day <= DateTime.DaysInMonth(currentDate.Year, currentDate.Month); day++) {
DateTime dt = new DateTime(currentDate.Year, currentDate.Month, day);
var shiftDay = shifts.Where(s => (int) dt.DayOfWeek - 1 == s.Day && s.IsActive).First();
if (shiftDay == null) {
// this is a non-working day because wasn't found in the shift List
}
}
答案 0 :(得分:2)
您应该使用DayOfWeek
枚举而不是int来使代码更具可读性并且更不容易出错:
List<DayOfWeek> workingDays = new List<DayOfWeek>() {
DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Friday
};
var nonWorkingDays = Enumerable.Range(1, 31)
.Select(x => new DateTime(2014, 7, x))
.Where(d => !workingDays.Contains(d.DayOfWeek))
.ToList();