I wish to create a LINQ query that will result in a resultset that contains a range of dates. I want to give it 2012-09-01 and 2012-09-05 and it should return a resultset containing:
- 2012-09-01
- 2012-09-02
- 2012-09-03
- 2012-09-04
- 2012-09-05
I want this because I wish to use it in a join for subsequent queries, that may not contain all dates. However, I want the final result to contain all dates in the interval, regardless of whether any of the other queries returned any results.
I use LINQ to Entities.
As a real example, it could look something like this:
from p in projects
where p.StartDate > dateFrom && p.StartDate < dateTo
// somehow 'select' every date between dateFrom and dateTo,
// so you get one row in the resultset for each date in between.
The easiest way to do this is to create a
Datestable:And fill it appropriately. T-SQL does not have arrays or generator functions.
The clustered index on this table will make it fast to select a small sub-range of all possible dates.
You can also add more columns like
DayOfWeek TINYINT NOT NULL,IsHoliday BIT NOT NULL, … Very handy.Let me also mention the possibility to fill in the missing dates in your application code. I think it depends on your exact situation if that is better or not. Especially if you want the joined results to use for further server-side processing you probably need a dates table.