Given:
var memberships = context.Memberships.OrderBy("it.CreateDate").ToList();
var monthlyCounts = from m in memberships
where m.CreateDate >= today.AddDays(-365) && m.CreateDate <= today
group m by m.CreateDate.Month
into g
select new
{
MemberCount = g.Count(m => m.UserId != null),
MembershipDate = g.Key,
};
I'm trying to join that with a list of months:
var months = Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
.Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m).Month);
This is what I came up with:
var cumlativeCounts = from d in months
join m in monthlyCounts on d equals m.MembershipDate into ms
from m in ms.DefaultIfEmpty()
select new
{
Month = d,
Count = m.MemberCount
};
Problem is I get an 'Object Reference not Set to an Instance of an Object' Exception, presumably on m.MemberCount
When I just use Count = m then I get all the properties from the monthlyCounts anonymous type, which is not exactly what I want.
Is there another way to outer join two anonymous types and reference a single property in the join query?
Thanks in advance.