I have DateStart, DateEnd Periodicity, TypePeriodicity fields.
We have a query:
var result = Events.Where(e => e.DateStart <=today && e.DateEnd >= today).ToList();
I want that this query to check Periodicity.
For example:
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):
I want the following, if current date equals:
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`
Thanks.
PS. Maybe not LINQ, but simple method that recieve result as parameter.
Here is something to get you started:
You could define a DateEvaluator delegate like so:
The purpose of the delegate would be to evaluate for a given periodicity type if a date should be considered as within range. We would have hence 3 date evaluators.
One for each period type: Lets call them
dayPeriodicityChecker,weekPeriodicityCheckerandmonthPeriodicityCheckerOur
dayPeriodicityCheckeris straightforward:Our
weekPeriodicityCheckerneeds to account for the start day of week, so the start date would need to be adjusted to the date in which the startDate week actually starts:Our
monthPeriodicityCheckerneeds to cater for months with a variable number of days:Once you have all your date evaluators defined you could put them in an array like so:
This will allow you to do :
So lets test this:
This will produce the desired output as below:
To use you would simply do:
Good luck!