В основном я делаю следующее:
// Tupple to hold data
IList<Tuple<DateTime, Int32>> data = new List<Tuple<DateTime, Int32>>();
// Get data from repository (Count by year) and add to Tupple
_repository.Set<Post>()
.Where(x => x.Created >= start && x.Created <= end)
.GroupBy(x => new { Year = x.Created.Year })
.Select(x => new { Year = x.Key.Year, Count = x.Count() })
.ToList().ForEach(x => data.Add(new Tuple<DateTime, Int32>(new DateTime(x.Year, 1, 1), x.Count)));
// Fill empty years with 0 as count
for (Int32 i = 0; i <= (end - start).TotalYears; i++)
if (!data.Any(x => x.Item1 == start.AddYears(i)))
data.Add(new Tuple<DateTime, Int32>(start.AddYears(i), 0));
// Resort data
data = data.OrderBy(x => x.Item1).ToList();
По сути, я искал способ сделать это за несколько шагов ...
Может быть, используя таблицу Union или Join ...
Я просто заполняю, я использую много шагов ...
Спасибо,
Miguel