title: Entity framework - Linq GroupBy data from multible tables
body: <p>I want to group data (using Northwind as an example) by Order.CustomerID.</p>
  
  <p>IN SQL it looks like this : </p>
  
  <pre><code>Select
    o.CustomerID,
    SUM(od.Quantity * od.UnitPrice * (1-od.Discount))
  
  FROM Orders o INNER JOIN [Order Details] od ON o.OrderID = od.OrderID
  GROUP BY o.CustomerID
  </code></pre>
  
  <p>How do I load that data using Linq?</p>
  
  <p>I tried</p>
  
  <pre><code>var order = context.Customers.Include("Orders.Order_Details")
              .SelectMany(x =&gt; x.Orders)
              .Select(x =&gt; new
              {
                  Customer = x.Customer,
                  Sum = x.Order_Details.Sum(y =&gt; (float)(y.Quantity * y.UnitPrice) * (1 - y.Discount))
              })
              .GroupBy(x =&gt; x.Customer)
              .Select(x =&gt; new
              {
                  x.Key.CompanyName,
                  Sum = x.Sum(y =&gt; y.Sum)
              });
  </code></pre>
  
  <p>But the query that's produced is ugly and too long.
  Any help will be appreciated.</p>