C# 事件重复(LINQ查询)
我有C# 事件重复(LINQ查询),c#,linq,C#,Linq,我有日期开始,日期结束周期,类型周期字段 我们有一个疑问: var result = Events.Where(e => e.DateStart <=today && e.DateEnd >= today).ToList(); 如果当前日期等于: 2,3,4,5 February - return `record1` 6,7,8..12 - not return, because TypePeriodicity = 1 and Periodicity =
日期开始
,日期结束
周期
,类型周期
字段
我们有一个疑问:
var result = Events.Where(e => e.DateStart <=today && e.DateEnd >= today).ToList();
如果当前日期等于:
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`
谢谢
注:可能不是LINQ,而是接收
结果
作为参数的简单方法。以下是一些让您开始学习的内容:
您可以这样定义DateEvaluator委托:
delegate bool DateEvaluator(DateTime startDate, DateTime endDate, DateTime dateToCheck, int periodicity);
DateEvaluator[] dateEvaluators = new DateEvaluator[]
{
dayPeriodicityChecker,
weekPeriodicityChecker,
monthPeriodicityChecker
};
代表的目的是评估某一日期是否应在规定范围内。因此,我们将有3名日期评估员。
每个期间类型一个:让我们调用它们dayporiodicitychecker
,weekPeriodicityChecker
和monthPeriodicityChecker
我们的dayPeriodicyChecker
非常简单:
DateEvaluator dayPeriodicityChecker = (startDate, endDate, dateToCheck, periodicity) =>
{
if ((dateToCheck < startDate) || (dateToCheck > endDate))
return false;
TimeSpan dateDiff = dateToCheck - startDate;
return dateDiff.Days % periodicity == 0;
};
我们的月周期检查需要满足不同天数的月:
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;
};
这将允许您执行以下操作:
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"));
}
}
祝你好运 嗯。。从您的示例中不太清楚您想要实现什么,也许您可以对您的示例进行一点扩展?如果DateStart
是2月21日,为什么与2,3,4,5 Feb
匹配?我想。。。着手问题是什么?我需要一个查询或方法来解析返回“record1”的“result”,如示例所示。现在,我的查询将返回“record1”,范围从2月2日到3月31日。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();