我有DateStart,DateEnd Periodicity,TypePeriodicity字段。
我们有一个问题:
var result = Events.Where(e => e.DateStart <=today && e.DateEnd >= today).ToList();我希望这个查询检查Periodicity。
例如:
name - record1
DateStart = 2012-02-02
DateEnd = 2012-03-31
Periodicity = 2
TypePeriodicity = 1 ( it's mean a week, may be also day = 0, month=2): 我想要以下内容,如果当前日期等于:
2,3,4,5 February - return `record1`
6,7,8..12 - not return, because TypePeriodicity = 1 and Periodicity = 2, which means every 2 weeks
13..19 - return `record1`
20..26 - not return
and so on until `DateEnd`谢谢。
PS。也许不是LINQ,而是接收result作为参数的简单方法。
发布于 2012-02-24 19:32:13
这里有一些东西可以让你入门:
您可以像这样定义一个DateEvaluator委托:
delegate bool DateEvaluator(DateTime startDate, DateTime endDate, DateTime dateToCheck, int periodicity);委托的目的是评估给定周期类型的日期是否应被视为在范围内。因此,我们将有3个日期计算器。每种周期类型一个:让我们称它们为dayPeriodicityChecker、weekPeriodicityChecker和monthPeriodicityChecker
我们的dayPeriodicityChecker很简单:
DateEvaluator dayPeriodicityChecker = (startDate, endDate, dateToCheck, periodicity) =>
{
if ((dateToCheck < startDate) || (dateToCheck > endDate))
return false;
TimeSpan dateDiff = dateToCheck - startDate;
return dateDiff.Days % periodicity == 0;
};我们的weekPeriodicityChecker需要考虑周的开始日期,因此需要将开始日期调整为startDate周实际开始的日期:
DateEvaluator weekPeriodicityChecker = (startDate, endDate, dateToCheck, periodicity) =>
{
if ((dateToCheck < startDate) || (dateToCheck > endDate))
return false;
DateTime adjustedStartDate = startDate.AddDays(-(int)startDate.DayOfWeek + 1);
TimeSpan dateDiff = dateToCheck - adjustedStartDate;
return (dateDiff.Days / 7) % periodicity == 0;
};我们的monthPeriodicityChecker需要用可变的天数满足几个月的需求:
DateEvaluator monthPeriodicityChecker dateToCheck, periodicity) =>
{
if ((dateToCheck < startDate) || (dateToCheck > endDate))
return false;
int monthDiff = 0;
while (startDate.AddMonths(1) < dateToCheck)
{
monthDiff++
// i'm sure there is a speedier way to calculate the month difference, but this should do for the purpose of this example
}
return (monthDiff - 1) % periodicity == 0;
};一旦你定义了所有的日期赋值器,你就可以将它们放在一个数组中,如下所示:
DateEvaluator[] dateEvaluators = new DateEvaluator[]
{
dayPeriodicityChecker,
weekPeriodicityChecker,
monthPeriodicityChecker
};这将允许您执行以下操作:
int periodicityType = 0; // or 1=week or 2=months
bool isDateIn = dateEvaluators[periodicityType ](startDate, endDate, dateTocheck, Periodicity)因此,让我们来测试一下:
PeriodicityEvent pEvent = new PeriodicityEvent
{
Name = "record1",
DateStart = new DateTime(2012, 02, 02),
DateEnd = new DateTime(2012, 03, 31),
PeriodicityType = 1,
Periodicity = 2
};
DateTime baseDate = new DateTime(2012, 02, 01);
for (int i = 0; i < 30; i++)
{
DateTime testDate = baseDate.AddDays(i);
if (dateEvaluators[pEvent.PeriodicityType](pEvent.DateStart, pEvent.DateEnd, testDate, pEvent.Periodicity))
{
Console.WriteLine("{0} is in", testDate.ToString("dd MMM"));
}
else
{
Console.WriteLine("{0} is out", testDate.ToString("dd MMM"));
}
}这将产生所需的输出,如下所示:

要使用您,只需执行以下操作:
Events.Where(e => dateEvaluators[e.PeriodType](e.DateStart, e.DateEnd, today, e.Periodicity).ToList();祝好运!
https://stackoverflow.com/questions/9428870
复制相似问题