title: Linq group by and sum with multiple tables
body: <p>I have two tables Orders and OrderDetails and i am trying to group by the username which is found in the orders table and then a sum of the quantities which is found in order details</p>
  
  <p>Orders table</p>
  
  <pre><code>|Order ID    |Username|
  |1           |User 1  |
  |2           |User 1  |
  |3           |User 2  |
  </code></pre>
  
  <p>OrderDetails Table</p>
  
  <pre><code>|OrderDetailsID| Qty| Order ID|
  |1             | 50 |1        |
  |2             | 20 |1        |
  |3             | 30 |2        | 
  |4             | 20 |3        |
  </code></pre>
  
  <p>How could i get the total quantity grouped by user using Linq?</p>
  
  <p>This is what i came up with </p>
  
  <pre><code>from order in Entity.Orders
  join od in Entity.OrderDetails on order.ID equals od.OrderID
  where order.Status != 2
  group order by new { order.Username } into users
  select new StatsiticsViewClients()
  {
       Username = users.Key.Username, 
       Qty = users.SelectMany(x =&gt; x.OrderDetails).Sum(x =&gt; x.Qty)
  }
  </code></pre>