Note that there are some explanatory texts on larger screens.

plurals
  1. POSql converted to Linq - Left Join, Group, Min and Sum
    primarykey
    data
    text
    <p>I have this simple piece of sql that does exactly what I need...</p> <pre><code>SELECT MIN(pb.Id) AS Id, MIN(pb.Quantity) AS Requested, SUM(pbi.Quantity) AS Total FROM PickBatchItems AS pb LEFT JOIN PickBatchItemLocations AS pbi ON pb.Id = pbi.PickBatchItemId GROUP BY pb.Id </code></pre> <p>Results in...</p> <pre><code>Id, Requested, Total 1 100 NULL 2 200 165 3 200 NULL </code></pre> <p>This is exactly what I want but I need this to be in Linq.</p> <p>So far I have...</p> <pre><code>var pick = (from pb in db.PickItems join pbi in db.PickItemLocations on pb.Id equals pbi.PickBatchItemId into TempJoin from resList in TempJoin.DefaultIfEmpty() where pb.Id == iPickItemId group resList by pb.Id into g select new { Id = g.Key, RequestedQuantity = g.Min(???????????????????????), SentQuantity = g.Sum(a =&gt; a.Quantity == null ? 0 : a.Quantity), }).FirstOrDefault(); </code></pre> <p>How can I get the <strong>RequestedQuantity</strong>?</p> <p><strong>UPDATE:</strong></p> <p>Thanks to 'David B' I have the answer:</p> <pre><code>var pick = (from pb in db.PickBatchItems join pbi in db.PickBatchItemLocations on pb.Id equals pbi.PickBatchItemId into TempJoin from resList in TempJoin.DefaultIfEmpty() where pb.Id == iPickItemId group new { PickItem = pb, PickItemLocation = resList } by pb.Id into g select new { Id = g.Key, RequestedQuantity = g.Min(a =&gt; a.PickItem.Quantity), SentQuantity = g.Sum(a =&gt; a.PickItemLocation.Quantity == null ? 0 : a.PickItemLocation.Quantity), }).FirstOrDefault(); </code></pre> <p>This joins and returns my two tables even if the PickBatchItemLocations table is empty.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload