Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would split the task up into three parts.</p> <p>First, I'd use LINQ to Entities to get the full collection of orders, each with it's corresponding collection of assignments:</p> <pre><code>var a = (from o in orders join a in assignments on s.Id equals a.OrderId into oa //Notice that I use oa.DefaultIfEmpty(). This essentially the way to do a //LEFT JOIN in LINQ. You'll want to do a LEFT JOIN if you don't //want to exclude order codes that have no assignments select new { o.OrderCode, o.Quantity, Assignments = oa.DefaultIfEmpty() }) .ToList(); </code></pre> <p><code>a</code> returns the following for your example:</p> <pre><code>OrderCode | Assignment 30000-1 | 4526 41000-7 | 2661 41000-7 | 5412 </code></pre> <p>Then I'd add the "missing" rows</p> <pre><code>var b = a.SelectMany(o =&gt; { var numOrdersInList = o.Count(o2 =&gt; o2.OrderCode == o.OrderCode); return Enumerable.Range(0, o.Quantity - numOrdersInList) .Select(i =&gt; new { o.OrderCode, Assignment = Enumerable.Empty&lt;Assignment&gt;() }); }); </code></pre> <p><code>b</code> returns the following for your example: </p> <pre><code>OrderCode | Assignment 30000-1 | 30000-1 | </code></pre> <p>Then I'd concat the two enumerables.</p> <pre><code>var c = a.Select(o =&gt; new { o.OrderCode, o.Assignment }) .Concat(b); </code></pre> <p>Finally, the concatenation should return what you expect for your example:</p> <pre><code>OrderCode | Assignment 30000-1 | 4526 30000-1 | 30000-1 | 41000-7 | 2661 41000-7 | 5412 </code></pre>
 

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