title: LINQ to SQL - Select SUM with a WHERE
body: <p>I'm having trouble getting a Linq to Sql query to work.  Basically, I want the sum of a column in a table for only rows matching a certain condition.  A simplified version is this:</p>
  
  <p>Orders = order that contains products
  OrderProducts = relational table containing products that are in an order
  Products = table containing information about products</p>
  
  <p>I can get the total sum of the qty of products like this:</p>
  
  <pre><code>Dim totalQty = requests.Sum(Function(p) p.OrderProducts.Sum(Function(q) CType(q.ProductQty, Nullable(Of Integer))))
  </code></pre>
  
  <p>(Requests is a IQueryable of Orders)</p>
  
  <p>But I also need to get the sum of qty where the actual product meets a certain condition.  Something like...</p>
  
  <pre><code>        Dim totalQty = requests.Sum(Function(p) p.OrderProducts.Sum(Function(q) CType(q.ProductQty, Nullable(Of Integer))))  
  </code></pre>
  
  <p>... WHERE p.OrderProducts.Product.ProductAttribute = true</p>
  
  <p>How would I go about getting that query with the additional where clause to only get the sum of the qtys where the productAttribute = true ???   Thanks!</p>