title: How to get Average of grouped results in Linq query
body: <p>I have a linq query that gets ratings from the previous 12 months:</p>
  
  <pre><code>var ratingsByMonth =
      from month in Enumerable.Range(0, 12)
      let key = new { Year = DateTime.Now.AddMonths(-month).Year, Month = DateTime.Now.AddMonths(-month).Month }
      join groupedRating in ratings on key
              equals new
              {
                  groupedRating.RatingDate.Year,
                  groupedRating.RatingDate.Month
              } into g
      select new {
          Date = key,
          Rating = g.Select(x=&gt;x.Rating)
      };
  </code></pre>
  
  <p>ratingsByMonth returns the following JSON from my data set:</p>
  
  <pre><code>{Date: {Year: 2016, Month: 11}, Rating: [4]}
  {Date: {Year: 2016, Month: 10}, Rating: [5, 5, 4]}
  {Date: {Year: 2016, Month: 9}, Rating: []}
  {Date: {Year: 2016, Month: 8}, Rating: []}
  {Date: {Year: 2016, Month: 7}, Rating: []}
  {Date: {Year: 2016, Month: 6}, Rating: []}
  {Date: {Year: 2016, Month: 5}, Rating: []}
  {Date: {Year: 2016, Month: 4}, Rating: []}
  {Date: {Year: 2016, Month: 3}, Rating: []}
  {Date: {Year: 2016, Month: 2}, Rating: []}
  {Date: {Year: 2016, Month: 1}, Rating: []}
  {Date: {Year: 2015, Month: 12}, Rating: []}
  </code></pre>
  
  <p>However I'm trying to aggregate the Rating field into an Average.  I've tried this:</p>
  
  <pre><code>return Json(new
  {
      Average = ratingsByMonth.Average(x=&gt;x.Rating),
      Dates = ratingsByMonth.Select(x =&gt; Date)
  });
  </code></pre>
  
  <p>But I get the following error highlighted on the Average - line:</p>
  
  <blockquote>
    <p>Cannot implicitly convert type
    'System.Collections.Generic.IEnumerable' to 'long?'</p>
  </blockquote>
  
  <p>The output that I am looking for is:</p>
  
  <pre><code>{Date: {Year: 2016, Month: 11}, Rating: 4}
  {Date: {Year: 2016, Month: 10}, Rating: 3.5}
  {Date: {Year: 2016, Month: 9}, Rating: 0}
  {Date: {Year: 2016, Month: 8}, Rating: 0}
  {Date: {Year: 2016, Month: 7}, Rating: 0}
  {Date: {Year: 2016, Month: 6}, Rating: 0}
  {Date: {Year: 2016, Month: 5}, Rating: 0}
  {Date: {Year: 2016, Month: 4}, Rating: 0}
  {Date: {Year: 2016, Month: 3}, Rating: 0}
  {Date: {Year: 2016, Month: 2}, Rating: 0}
  {Date: {Year: 2016, Month: 1}, Rating: 0}
  {Date: {Year: 2015, Month: 12}, Rating: 0}
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Have tried changing to the following:</p>
  
  <pre><code>return Json(new
  {
      Average = ratingsByMonth.Select(x =&gt; x.Rating).SelectMany(x =&gt; x).Average(),
      Dates = ratingsByMonth.Select(x =&gt; x.Date.Month)
  });
  </code></pre>
  
  <p>However, this just gives me a single 'Average' value across the whole data set, rather than an average per date.</p>
  
  <p>Edit:</p>
  
  <p>Here is sample data:</p>
  
  <pre><code>RatingID    Rating  RatingDate
  5           5   2016-10-09 20:11:29.590
  11          0   2016-10-09 20:14:42.503
  21          5   2016-10-09 20:24:15.667
  60          4   2016-10-28 11:01:21.220
  64          4   2016-11-03 16:30:47.657
  </code></pre>
  
  <p>So for November the average should be 4, and October should be 3.5 (5+0+5+4 / 4)</p>