在 EF Core 5 中按过去 12 个月对记录进行分组时如何得到空月

How can I get empty months while grouping records by last 12 month in EF Core 5

我有下面的代码片段,用于对过去 12 个月的记录进行分组,它工作正常,但我只是注意到不包括空月份。我哪里做错了? 提前致谢

public IQueryable<DashboardGrouping> DashboardStats()
{
   var yearAgo = DateTime.Now.AddYears(-1);
   var date = new DateTime(yearAgo.Year, yearAgo.Month, 1);

   var items = context.Set<Transaction>()
   .Where(x => x.IsActive &&
      x.CreatedAt.HasValue && x.CreatedAt.Value.Date >= date.Date && x.PaymentStatusId == 
      PaymentStatus.Completed)
   .Include(x => x.Payment)
   .Include(x => x.Branch)
   .AsNoTracking()
   .Select(x => new
   {
      Year = x.CreatedAt.Value.Year,
      Month = x.CreatedAt.Value.Month,
      CashAmount = x.Payment.CashAmount,
      CardAmount = x.Payment.CardAmount,
   })
   .GroupBy(x => new
   {
      Year = x.Year,
      Month = x.Month,
   })
   .Select(x => new DashboardGrouping
   {
      Year = x.Key.Year,
      Month = x.Key.Month,
      TotalSale = x.Sum(s => s.CashAmount + s.CardAmount)
   });
   return items; 
}

您可以进行客户端后处理并使用缺失记录丰富结果。

生成月份的辅助函数:

static IEnumerable<DateTime> GetMonths(DateTime startDate, DateTime endDate)
{
    startDate = new DateTime(startDate.Year, startDate.Month, 1);
    endDate = new DateTime(endDate.Year, endDate.Month, 1);

    while (startDate < endDate)
    {
        yield return startDate;
        startDate = startDate.AddMonths(1);
    }
}

后处理:

var currentDate = DateTime.Now;
var yearAgo = currentDate.AddYears(-1);

var months = GetMonths(yearAgo, currentDate);

var stats = DashboardStats().ToList();

// left join months to actual data
var query =
    from m in months
    join s in stats on new { m.Year, m.Month } equals new { s.Year, s.Month } into gj
    from s in gj.DefaultIfEmpty()
    select s ?? new DashboardGrouping
    {
        Year = m.Year,
        Month = m.Month,
        TotalSale = 0
    };

var result = query.ToList();