在DayOfWeek列表中找到最近的工作日 [英] Find Closest Week Day in DayOfWeek List

查看:86
本文介绍了在DayOfWeek列表中找到最近的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个新手问题,但这是可行的.

This is probably a newbie question, but here goes.

我有一种方法,其中将类型DayOfWeek List附加到一周的不同日期(可以是星期三&周六,周六,周日,星期一和&星期五等).

I have a method where a type DayOfWeek List that gets appended with various days of the week (Could be Wednesday & Saturday, Sunday, Monday, & Friday, etc).

给出该列表,我需要将其与Datetime参数进行比较,在DayOfWeek列表中找到DateTime参数最接近的工作日,并根据列表中的工作日将天添加到DateTime参数中.

Given that list, I need to compare it to a Datetime parameter, find the Week Day the DateTime parameter is closest to in the DayOfWeek list, and add days to the DateTime parameter based on what Week Day it is in the list.

例如,如果传入的DateTime参数是星期天,而我的DayOfWeek列表包含星期三和星期六,则该参数必须移回星期六,因为它在列表中最接近.

For example, if the DateTime parameter being passed in is a Sunday, and my DayOfWeek list contains a Wednesday and Saturday, the parameter needs to be moved back to Saturday since it is closest in the list.

类似地,如果我的列表包含星期天,星期一和星期六,并且传入的参数是星期四,则必须将参数移到星期六.

Similarly, if my list contains Sunday, Monday, and Saturday, and the parameter passed in is Thursday, then the parameter would have to be moved to Saturday.

最后,如果参数与列表中的两周天是等距的(传递星期三,列表中包含星期一和星期五...或传递星期日,并且列表中包含星期二和星期五),则参数需要向前移动到下一个最接近的工作日(在第一种情况下为星期五,在第二种情况下为星期二).

Finally, if the parameter is equidistant from two week days in the list (Wednesday is passed in and Monday and Friday are in the list... or Sunday is passed in and Tuesday and Friday are in the list), then the parameter needs to be moved forward to the next closest week day (which, in the first case, would be Friday, and Tuesday in the second case).

(至少对我而言)将下一个最近的工作日从传入日期转换为int的距离是理想的,这样我可以执行以下操作:

It would be ideal (at least for me), to convert the distance of the next closest week day from the passed in date to an int, that way I can do something like:

passedInDate = passedInDate.AddDays(dayOfWeekDistance);
return passedInDate;

但我愿意提出建议.

我尝试了LINQ语句,例如:

I have tried LINQ statements such as:

int dayOfWeekDistance = targetDayOfWeekList.Min(x => (x - passedInDate));

但无济于事.我必须缺少一些花哨的LINQ语句.

But to no avail. There has to be some fancy LINQ statements that I'm missing.

请注意,如果传递的日期是周日,并且列表中最近的工作日是周六,那么我无法工作的主要项目是将日期从星期日回溯到周六.传入的日期是星期一,而最近的工作日是星期五,则该日期需要一直追溯到星期五).

Just a heads up, the main item I can't get to work is for the date to backtrack from Sunday back to Saturday if the passed in date is Sunday and the closest week day in the list is Saturday (similarly, if the passed in date is Monday and the closest week day is Friday, the date would need to traverse all the way back to Friday).

请让我知道我是否错过了任何事情,或者我只是说不通.

Please let me know if I missed anything or I'm just plain not making sense.

欢迎所有帮助!谢谢.

推荐答案

通过辅助功能,可以使用LINQ.

With a helper function, LINQ can be used.

helper函数使用实用程序函数计算两个DOW之间的远期天数来计算最接近的星期几:

The helper function computes the closest day of week using a utility function to compute the number of forward days between the two DOWs:

public int MinDOWDistance(DayOfWeek dow1, DayOfWeek dow2) {
    int FwdDaysDiff(int idow1, int idow2) => idow2 - idow1 + ((idow1 > idow2) ? 7 : 0);
    int fwd12 = FwdDaysDiff((int)dow1, (int)dow2);
    int fwd21 = FwdDaysDiff((int)dow2, (int)dow1);
    return fwd12 < fwd21 ? fwd12 : -fwd21;
}

然后,您可以在列表中找到最近的DOW,并使用带有LINQ的Aggregate返回正确的天数(和方向):

Then you can find the nearest DOW in the list and return the right number of days to move (and direction) using Aggregate with LINQ:

public int DaysToClosestDOW(DayOfWeek dow1, List<DayOfWeek> dowList) {
    return dowList.Select(dow => {
                                    var cdow = MinDOWDistance(dow1, dow);
                                    return new { dow, dist = cdow, absdist = Math.Abs(cdow) };
                                 })
                  .Aggregate((g1, g2) => (g1.absdist < g2.absdist) ? g1 : ((g1.absdist == g2.absdist) ? ((g1.dist > 0) ? g1 : g2) : g2)).dist;
}

在我看来,我可以使用元组从帮助器函数中返回absdist,因为它已经知道了.然后,我可以在LINQ中使用Tuple:

It occurred to me I could use a tuple to return the absdist from the helper function since it already knows it. Then I can just use the Tuple in the LINQ:

public (int dist, int absdist) MinDOWDistance(DayOfWeek dow1, DayOfWeek dow2) {
    int FwdDaysDiff(int idow1, int idow2) => idow2 - idow1 + ((idow1 > idow2) ? 7 : 0);
    int fwd12 = FwdDaysDiff((int)dow1, (int)dow2);
    int fwd21 = FwdDaysDiff((int)dow2, (int)dow1);
    if (fwd12 < fwd21)
        return (fwd12, fwd12);
    else
        return (-fwd21, fwd21);
}

public int DaysToClosestDOW(DayOfWeek dow1, List<DayOfWeek> dowList) {
    return dowList.Select(dow => MinDOWDistance(dow1, dow))
                  .Aggregate((g1, g2) => (g1.absdist < g2.absdist) ? g1 : ((g1.absdist == g2.absdist) ? ((g1.dist > 0) ? g1 : g2) : g2)).dist;
}

这篇关于在DayOfWeek列表中找到最近的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆