0

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.

1 Answer 1

1

You can write

m == null ? 0 : m.MemberCount
Sign up to request clarification or add additional context in comments.

1 Comment

that was to easy for you gahhh! >.< I had something similar m.MemberCount != null ? m.MemberCount : 0 of course it did not work =\ Thanks man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.