IE无数,除了不工作

本文关键字:工作 无数 IE | 更新日期: 2023-09-27 18:31:07

>我有以下代码

/// <summary>
///     Calculates the date with a number of working days offset from today by
///     calculating a set of dates that exclude weekends and holidays, then
///     skipping forward the required number of days.
/// </summary>
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;
    var publicHolidays = GetPublicHolidays();
    var daysWorking = workDaysQuery.Except(publicHolidays).ToArray(); // why does this still include the publicHolidays ?
    return daysWorking.Skip(workingDays).First();
}

但是,"除外"不会删除公共假期。

公共假日日期的时间部分均为零。

IE无数,除了不工作

试试这个:

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内部完成)将是正确的。