I'm trying to get a dataset for an SSRS Line Chart. I am trying to show the number of records per week of the year for the last six months. In order for the line chart to be accurate, I have to show weeks that have zero records.
I want to provide the line chart with data that looks like this:
Year Week Count
2014 52 13
2015 1 0
2015 2 16
The following linq query gets everything I need minus the weeks that have zero records:
list = (from t in context.Cats.Where(
t => t.Name == "Fluffy" &&
(t.Recorded >= start && t.Recorded <= end)
).AsEnumerable()
group t by new { Year = t.Recorded.Year, WeekNumber = (t.Recorded - new DateTime(t.Recorded.Year, 1, 1)).Days / 7 } into ut
select new TrendRecord
{
Year = ut.Key.Year,
Week = ut.Key.WeekNumber,
Count = ut.Count()
}).OrderBy(t => t.Year).ThenBy(t => t.Week).ToList<TrendRecord>();
I've looked at this and this SO questions but neither seems to quite fit my predicament.
I'm leaning towards creating a list with all possible week numbers between the start and end dates and then left joining my data to it somehow so that weeks with 0 records show up.
Is this an acceptable approach to this problem(please show me how if so)? Is there a way to do this in one query without the need for a separate list to join to?