我有以下代码
var
然而,除非取消公众假期。
公众假期的时间部分均为零。
答案 0 :(得分:3)
试试这个:
public static DateTime AddWorkingDays(int workingDays, DateTime startDate)
{
var workDaysQuery = from n in Enumerable.Range(0, (workingDays + 14) * 2)
let date = startDate.AddDays(n)
where (date.DayOfWeek != DayOfWeek.Sunday)
&& (date.DayOfWeek != DayOfWeek.Saturday)
select date.Date; //Remove the time component
//The following change is not needed if you know for sure that
//values returned by GetPublicHolidays() will not include a time component
var publicHolidays = GetPublicHolidays().Select(x => x.Date);
var daysWorking = workDaysQuery.Except(publicHolidays).ToArray();
return daysWorking.Skip(workingDays).First();
}
基本上,如果两个DateTime
值具有相同的日期(年,月和日),但时间分量不同,则系统会将它们视为不相等。
上面的代码是什么,它使用Date
属性只获取日期组件(没有时间)。这样,相等性检查(由Except
在内部完成)将是正确的。