title: Using LINQ to aggregate and group a list of data into a new list but Sum is 0
body: <p>I have a list of data retrieved from SQL and stored in a class. I want to now aggregate the data using LINQ in C# rather than querying the database again on a different dataset.</p>
  <p><a href="https://i.stack.imgur.com/E9Ade.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E9Ade.png" alt="enter image description here" /></a></p>
  <p>Example data I have is above.
  Date, Period, Price, Vol and I am trying to create a histogram using this data. I tried to use Linq code below but seem to be getting a 0 sum.</p>
  <ol>
  <li>Period needs to be a where clause based on a variable</li>
  <li>Volume needs to be aggregated for the price ranges</li>
  <li>Price needs to be a bucket and grouped on this column</li>
  </ol>
  <p>I dont want a range. Just a number for each bucket.</p>
  <p>Example output I want is (not real data just as example):</p>
  <pre><code>Bucket    SumVol
  18000       50
  18100       30
  18200       20
  </code></pre>
  <p>Attempted the following LINQ query but my SUM seems to be be empty. I still need to add my where clause in, but for some reason the data is not aggregating.</p>
  <pre><code>var ranges = new[] { 10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000 };
  var priceGroups = eod.GroupBy(x =&gt; ranges.FirstOrDefault(r =&gt; r &gt; x.price))
      .Select(g =&gt; new { Price = g.Key, Sum = g.Sum(s =&gt; s.vol)})
      .ToList();
  
  var grouped = ranges.Select(r =&gt; new
  {
      Price = r,
      Sum = priceGroups.Where(g =&gt; g.Price &gt; r || g.Price == 0).Sum(g =&gt; g.Sum)
  });
  </code></pre>