score:1

Accepted answer
  1. Add the following to your query:

    .GroupBy(x => x.Problem)
    .ToDictionary(g => g.Key, g => g.Select(x=>new { Year=x.Year, Quarter=x.Quarter, Count = x.Count }));
    
  2. You have to insert the following before .ToDictionary() above:

    .Select(g =>
      new { 
        Key = g.Key, 
        Items = 
          g
            .GroupBy(r => r.Year)
            .SelectMany(gy =>
              gy.Concat(
                Enumerable.Range(1,5)
                  .Where(q => !gy.Any(r=>r.Quarter == q))
                  .Select(q => new { Problem = g.Key, Year = gy.Key, Quarter = q, Count = 0 })
              )
            )
      }
    )
    

I think... try it out :)

I would advise against following this approach, however, and create "empty" records on the client, to avoid excessive bandwidth use.

score:2

Thanks to Mr. TA for the inspiration and for showing me that you can use LINQ against a grouping.

I have tested this out in a local environment and the LINQ does indeed return a list of Problems tied to an array of Year/Quarter groupings with a total Count. I don't know if Json.Encode encodes it in the correct format though.

The following LINQ should return an anonymous type that fits the format you needed:

Edit: Query now returns count=0 for quarters where at least one problem occurred, but specified problem did not occur

var quarters = RMAs
    .Where(rma => rma.CreatedDate.Year > DateTime.Now.Year - 4)
    .GroupBy(rma => new { 
        Year = rma.CreatedDate.Year, 
        Quarter = ((rma.CreatedDate.Month) / 3) 
    });

return Json.Encode(
    RMAs
        //Only grab the last 4 years worth of RMAs
        .Where(r => r.CreatedDate.Year > DateTime.Now.Year - 4)
        // Group all records by problem     
        .GroupBy(r => new { Problem = r.Problem })
        .Select(grouping => new
            {
                Problem = grouping.Key.Problem,
                Occurrences = quarters.Select(quarter => new
                    {
                        Year = quarter.Key.Year,
                        Quarter = quarter.Key.Quarter,
                        Count = grouping
                                .GroupBy(record => new
                                {
                                    Year = record.CreatedDate.Year,
                                    Quarter = ((record.CreatedDate.Month) / 3)
                                })
                                .Where(record => 
                                    record.Key.Year == quarter.Key.Year 
                                    && record.Key.Quarter == quarter.Key.Quarter
                                ).Count()
                    }).ToArray()
            }));

Update: Thanks to JamieSee for updating with example JSON output:

This is an example of the JSON output:

[{"Problem":"P","Occurrences":[{"Year":2012,"Quarter":4,"Count":2},{"Year":2012,"Quarter":2,"Count":1},{"Year":2012,"Quarter":1,"Count":1}]},{"Problem":"Q","Occurrences":[{"Year":2012,"Quarter":3,"Count":1},{"Year":2012,"Quarter":2,"Count":1},{"Year":2012,"Quarter":1,"Count":1}]}]

score:0

Here's the full restatement to meet all your criteria:

public static IEnumerable<DateTime> GetQuarterDates()
{
    for (DateTime quarterDate = DateTime.Now.AddYears(-4); quarterDate <= DateTime.Now; quarterDate = quarterDate.AddMonths(3))
    {
        yield return quarterDate; 
    }
}

public static void RunSnippet()
{
    var RMAs = new[] {
        new { Problem = "P", CreatedDate = new DateTime(2012, 6, 2) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 12, 7) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 12, 8) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 8, 1) },
        new { Problem = "P", CreatedDate = new DateTime(2011, 4, 1) },
        new { Problem = "Q", CreatedDate = new DateTime(2011, 11, 11) },
        new { Problem = "Q", CreatedDate = new DateTime(2011, 6, 6) },
        new { Problem = "Q", CreatedDate = new DateTime(2011, 3, 3) }
    };

    var quarters = GetQuarterDates().Select(quarterDate => new { Year = quarterDate.Year, Quarter = Math.Ceiling(quarterDate.Month / 3.0) });

    var rmaProblemQuarters = from rma in RMAs
            where rma.CreatedDate > DateTime.Now.AddYears(-4)
            group rma by rma.Problem into rmaProblems
            select new {
                            Problem = rmaProblems.Key,
                            Quarters = (from quarter in quarters
                                        join rmaProblem in rmaProblems on quarter equals new { Year = rmaProblem.CreatedDate.Year, Quarter = Math.Ceiling(rmaProblem.CreatedDate.Month / 3.0) } into joinedQuarters
                                        from joinedQuarter in joinedQuarters.DefaultIfEmpty()
                                        select new {
                                                        Year = quarter.Year,
                                                        Quarter = quarter.Quarter,
                                                        Count = joinedQuarters.Count()
                                                   })
                       };

    string json = System.Web.Helpers.Json.Encode(rmaProblemQuarters);
    Console.WriteLine(json);
}

Which yields:

[{"Problem":"P","Quarters":[{"Year":2008,"Quarter":2,"Count":0},{"Year":2008,"Quarter":3,"Count":0},{"Year":2008,"Quarter":4,"Count":0},{"Year":2009,"Quarter":1,"Count":0},{"Year":2009,"Quarter":2,"Count":0},{"Year":2009,"Quarter":3,"Count":0},{"Year":2009,"Quarter":4,"Count":0},{"Year":2010,"Quarter":1,"Count":0},{"Year":2010,"Quarter":2,"Count":0},{"Year":2010,"Quarter":3,"Count":0},{"Year":2010,"Quarter":4,"Count":0},{"Year":2011,"Quarter":1,"Count":0},{"Year":2011,"Quarter":2,"Count":1},{"Year":2011,"Quarter":3,"Count":1},{"Year":2011,"Quarter":4,"Count":2},{"Year":2011,"Quarter":4,"Count":2},{"Year":2012,"Quarter":1,"Count":0},{"Year":2012,"Quarter":2,"Count":1}]},{"Problem":"Q","Quarters":[{"Year":2008,"Quarter":2,"Count":0},{"Year":2008,"Quarter":3,"Count":0},{"Year":2008,"Quarter":4,"Count":0},{"Year":2009,"Quarter":1,"Count":0},{"Year":2009,"Quarter":2,"Count":0},{"Year":2009,"Quarter":3,"Count":0},{"Year":2009,"Quarter":4,"Count":0},{"Year":2010,"Quarter":1,"Count":0},{"Year":2010,"Quarter":2,"Count":0},{"Year":2010,"Quarter":3,"Count":0},{"Year":2010,"Quarter":4,"Count":0},{"Year":2011,"Quarter":1,"Count":1},{"Year":2011,"Quarter":2,"Count":1},{"Year":2011,"Quarter":3,"Count":0},{"Year":2011,"Quarter":4,"Count":1},{"Year":2012,"Quarter":1,"Count":0},{"Year":2012,"Quarter":2,"Count":0}]}]

Related Articles