title: Getting the sum of a value in Linq
body: <p>Given the following structure...</p>
  
  <pre><code>class Foo {
  
    public string Category { get; set; }
    public string Code { get; set; }
    public int Quantity { get; set; }
  
  }
  </code></pre>
  
  <p>I am trying to use Linq to summarise a list of these objects by Category and Code, so that if I provide the following source...</p>
  
  <pre><code>List&lt;Foo&gt; foos = new List&lt;Foo&gt;() {
    new Foo {Category = @"A", Code = @"B", Quantity = 1},
    new Foo {Category = @"A", Code = @"B", Quantity = 2},
    new Foo {Category = @"C", Code = @"D", Quantity = 3},
    new Foo {Category = @"C", Code = @"D", Quantity = 4}
  };
  </code></pre>
  
  <p>I end up with a list containing...</p>
  
  <pre><code>  Foo {Category = @"A", Code = @"B", Quantity = 3},
    Foo {Category = @"C", Code = @"D", Quantity = 7}
  </code></pre>
  
  <p>(where Quantity is the sum of the original quantities of matching objects).</p>
  
  <p>I know I need to use a combination of the <code>group by</code> clause and the <code>Sum()</code> extension methods, I just do not seem to be able to find that correct combination.</p>
  
  <p>Please note, there is no database behind this list, I am doing this purely with objects so extracting the original list including the sum is not an option.</p>
  
  <p>Thanks.</p>