例如,我将星期一、星期四和星期日作为抽奖日期。使用 Linq Lambda 查找与 Datetime.Now 最接近的 dayOfWeek 的方法是什么?我使用正常功能完成了此操作,但我想知道如何使用 Linq Lambda 来完成此操作?
我的方法示例:
public static DateTime GetSoonestDrawDate(this DateTime from, IEnumerable<LotteryDrawDate> drawDates)
{
int hour = 0;
int minute = 0;
bool todayDrawOnly = true;
int difference = 7;
int tempDifference = 0;
int todayDay = (int)from.DayOfWeek;
int drawCutOffDay = 0;
if (todayDay == 0)
{
todayDay = 7;
}
var tempCutOffTime = new TimeSpan(23, 59, 59);
foreach (var drawDate in drawDates)
{
// DayId is DayOfWeek
drawCutOffDay = drawDate.DayId;
if (drawCutOffDay < todayDay)
{
drawCutOffDay += 7;
}
tempDifference = drawCutOffDay - todayDay;
var cutOffTime = new TimeSpan();
cutOffTime = TimeSpan.Parse(drawDate.CutOffTime);
if ((difference > tempDifference) || difference == 0)
{
// draw date is not today
if (tempDifference != 0)
{
difference = tempDifference;
hour = cutOffTime.Hours;
minute = cutOffTime.Minutes;
todayDrawOnly = false;
}
// same day, cutOffTime still available
if ((tempDifference == 0 && cutOffTime > from.TimeOfDay))
{
// we use tempCutOffTime to compare newest cutOffTime in case we have more cutOffTimes on the same day
// in that case we have to select the soonest cutOffTime of the day
if (cutOffTime < tempCutOffTime)
{
difference = tempDifference;
hour = cutOffTime.Hours;
minute = cutOffTime.Minutes;
todayDrawOnly = true;
tempCutOffTime = cutOffTime;
}
}
// same day selected only, but cutOffTime passed, so we add another week only in case there is only one draw date and draw date is on the same day
else if (tempDifference == 0 && cutOffTime < from.TimeOfDay && todayDrawOnly == true)
{
if (cutOffTime < tempCutOffTime)
{
difference = 7;
hour = cutOffTime.Hours;
minute = cutOffTime.Minutes;
todayDrawOnly = true;
tempCutOffTime = cutOffTime;
}
}
}
}
from = from.AddDays(difference);
DateTime soonest = new DateTime(from.Year, from.Month, from.Day, hour, minute, 0);
return soonest;
}
因为你没有展示如何LotteryDrawDate
看起来像,我使用准备了一个小样本DayOfWeek
仅有的。您必须扩展它以自己查看时间部分。
public static DateTime GetSoonestDrawDate(this DateTime from, IEnumerable<DayOfWeek> drawDates)
{
var realDrawDates = drawDates.SelectMany(x => new[] { (int)x, (int)x + 7 }).OrderBy(x => x);
var difference = realDrawDates.SkipWhile(x => x < (int)from.DayOfWeek).First() - (int)from.DayOfWeek;
return from.AddDays(difference);
}
小测试代码:
var drawDates = new[] { DayOfWeek.Monday, DayOfWeek.Wednesday, DayOfWeek.Saturday };
for (int i = 0; i < 15; i++)
{
var from = DateTime.Now.AddDays(i);
Console.WriteLine("{0} - {1}", from.ToShortDateString(), GetSoonestDrawDate(from, drawDates).ToShortDateString());
}
prints (从 - 下一个):
11/18/2013 - 11/18/2013
11/19/2013 - 11/20/2013
11/20/2013 - 11/20/2013
11/21/2013 - 11/23/2013
11/22/2013 - 11/23/2013
11/23/2013 - 11/23/2013
11/24/2013 - 11/25/2013
11/25/2013 - 11/25/2013
11/26/2013 - 11/27/2013
11/27/2013 - 11/27/2013
11/28/2013 - 11/30/2013
11/29/2013 - 11/30/2013
11/30/2013 - 11/30/2013
12/1/2013 - 12/2/2013
12/2/2013 - 12/2/2013
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)