title: C#: How to use the Enumerable.Aggregate method
body: <p>Lets say I have this amputated <code>Person</code> class:</p>
  
  <pre><code>class Person
  {
      public int Age { get; set; }
      public string Country { get; set; }
  
      public int SOReputation { get; set; }
      public TimeSpan TimeSpentOnSO { get; set; }
  
      ...
  }
  </code></pre>
  
  <p>I can then group on <code>Age</code> and <code>Country</code> like this:</p>
  
  <pre><code>    var groups = aListOfPeople.GroupBy(x =&gt; new { x.Country, x.Age });
  </code></pre>
  
  <p>Then I can output all the groups with their reputation totals like this:</p>
  
  <pre><code>foreach(var g in groups)
      Console.WriteLine("{0}, {1}:{2}", 
          g.Key.Country, 
          g.Key.Age, 
          g.Sum(x =&gt; x.SOReputation));
  </code></pre>
  
  <p>My question is, how can I get a sum of the <code>TimeSpentOnSO</code> property? The <code>Sum</code> method won't work in this case since it is only for <code>int</code> and such. I thought I could use the <code>Aggregate</code> method, but just seriously can't figure out how to use it... I'm trying all kinds properties and types in various combinations but the compiler just won't recognize it.</p>
  
  <pre><code>foreach(var g in groups)
      Console.WriteLine("{0}, {1}:{2}", 
          g.Key.Country, 
          g.Key.Age, 
          g.Aggregate(  what goes here??  ));
  </code></pre>
  
  <p>Have I completely missunderstood the Aggregate method? Or what is going on? Is it some other method I should use instead? Or do I have to write my own <code>Sum</code> variant for <code>TimeSpan</code>s?</p>
  
  <p>And to add to the mess, what if Person is an anonymous class, a result from for example a <code>Select</code> or a <code>GroupJoin</code> statement?</p>
  
  <hr>
  
  <p>Just figured out that I could make the <code>Aggregate</code> method work if I did a <code>Select</code> on the <code>TimeSpan</code> property first... but I find that kind of annoying... Still don't feel I understand this method at all...</p>
  
  <pre><code>foreach(var g in groups)
      Console.WriteLine("{0}, {1}:{2}", 
          g.Key.Country, 
          g.Key.Age, 
          g.Select(x =&gt; x.TimeSpentOnSO)
          g.Aggregate((sum, x) =&gt; sum + y));
  </code></pre>