I'm getting data from db like this:
Later I group them so it looks like
Month 6
Week 2
Amount 228
And so on..
Here is the code:
var yas = await _context.product
.AsNoTracking()
.Where(x => (x.PaymentDate != null && x.PaymentDate > DateTime.UtcNow.AddMonths(-4))).ToListAsync();
var grouped = yas.GroupBy(x => CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(x.PaymentDate ?? DateTime.UtcNow, CalendarWeekRule.FirstDay, DayOfWeek.Monday))
.Select(product => new productsDemoObject
{
Week = GetWeekNumberOfMonth(product.FirstOrDefault().PaymentDate.Value),
Amount = product.Sum(x => x.Amount),
Month = product.FirstOrDefault().PaymentDate.Value.Month
});
As you can see for Month 6 there is only data for week 2. And it's group and work as expected, but now I'm wondering how could I add empty object with amount of 0 for missing weeks.
For example if there is only week 2, lets add data with amount 0 for week 1,3 and 4.
In example of Month 8, because there are weeks 2 and 3 I should add week 1 and 4 with amount of 0.
How could I achieve this?
Thanks guys
Cheers