title: "Value Cannot Be Null" when using LINQ to SQL to Group By and Sum results from a WCF Service request
body: <p>I'm trying to use LINQ to SQL to sum a row in a a resultset returned from a WCF service. I'm able to use LINQ for basic selects, for order by, for filtering, etc.  So far so good.</p>
  
  <p>But when I try to Group By / Sum, I get an error message "Value Cannot Be Null" when trying to Take(5) from the query below.  (<code>results</code> is non-null and has 1000+s of items, but the <code>query</code> is null when we try to return it:) </p>
  
  <p><strong>EDIT:</strong> I've removed the invalid cast as mentioned in the answer below, instead selecting the aggregate results into a custom class written to order.  Now the page runs without error -- but the group by / sum doesn't work.... I just get the top five highest values in the results, not summed by group.    Why?</p>
  
  <pre><code>var query = (from h in results
              group h by h.Order into hh                     
              select new TopOrders()
              {
                  Order = hh.Key,
                  Total= hh.Sum(r =&gt; r.Price)
              }).OrderByDescending(i =&gt; i.Total);
  
  return query.Take(5).ToList();
  </code></pre>
  
  <p>Only possible wrinkle I can point to is that Order is itself a collection (OrderID, OrderName, etc.) and so I've tried grouping explicitly on hh.Key.Order.OrderID and so forth, to no effect.</p>
  
  <p>Anything I'm missing from what you can see?   Thanks.</p>