score:2

Accepted answer

Here is the basic gist of what I mentioned in the comments.

var model = data.GroupBy(_ => _.Section.SectionName)
    .Select(section => new MyMainViewModel {
        Location = section.Key,
        Children = section.GroupBy(_ => new {
            Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(_.DateEntered.Month),
            Year = _.DateEntered.Year.ToString()
        }).Select(period => new MyChildViewModel {
            Month = period.Key.Month,
            Year = period.Key.Year,
            TotalPhoneCalls = period.Sum(_ => _.PhoneCallsTaken)
        }).ToList()
    });

You initially grouped by section. Now you additionally want to group the sections by month and year and then sum the calls for those groups.

score:0

Your current LINQ:

var data = db.TestRecords.ToList();

IEnumerable<MyMainViewModel> model = data.GroupBy(x => x.Section.SectionName).Select(y => new MyMainViewModel(){  
     Location = y.Key,
     Children = y.Select(z => new MyChildViewModel(){
          Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(z.DateEntered.Month),
          Year = z.DateEntered.Year.ToString(),
          TotalPhoneCalls = y.Where(t => t.Section.SectionName == z.Section.SectionName).Sum(t => t.PhoneCallsTaken)
     }).Distinct()
 });

Slightly Tweaked:

var data = db.TestRecords.Select(d => d.SecId, d.DateEntered, d.PhoneCallsTaken).ToList();

IEnumerable<MyMainViewModel> model = data.GroupBy(x => x.Section.SectionName).Select(y => new MyMainViewModel(){  
     Location = y.Key,
     Children = y.Select(z => new MyChildViewModel(){
          Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(z.DateEntered.Month),
          Year = z.DateEntered.Year.ToString(),
          TotalPhoneCalls = y.Where(t => t.Section.SectionName == z.Section.SectionName).Sum(t => t.PhoneCallsTaken)
     }).Distinct()
 });

Modify the data variable assignment for your needs and make sure the changes are reflected in your later use of the List.


Related Query

More Query from same tag