title: c# / Linq sum where
body: <p>I have a table NCR containing data of the format:</p>
  
  <pre><code>ID | Date     | Item   | Type | Qty
  1  | 01/01/13 | Apple  | A    | 1 
  2  | 01/01/13 | Apple  | B    | 1 
  3  | 01/01/13 | Orange | C    | 1 
  4  | 01/01/13 | Orange | A    | 2 
  6  | 01/01/13 | Orange | C    | 1 
  </code></pre>
  
  <p>I would like to produce a linq query that gives me a summary of the types and sums for a given date like so:</p>
  
  <pre><code>Item   | A | B | C
  Apple  | 1 | 1 | 0 
  Orange | 2 | 0 | 2 
  </code></pre>
  
  <p>So far I have this:</p>
  
  <pre><code>var q = data.GroupBy(l =&gt; l.Item)
               .Select(g =&gt; new {
                                      Item = g.Key,
                                      Total = g.Sum(c =&gt; c.Qty),
                                      A = g.Sum(c =&gt; c.Type == "A"),
                                      B = g.Sum(c =&gt; c.Type == "B"),
                                      C = g.Sum(c =&gt; c.Type == "C")
  });
  </code></pre>
  
  <p>However I can't seem to give a criteria to the g.Sum lambda statement. If I use Count (which is the wrong data) I can give the critera, but why is Sum missing this? What is my alternative to creating a summary table of the data available?</p>