title: Order list by parent and child items
body: <p>I have a list of products that have to be ordered by parent then all children of that parent, then next parent, etc.</p>
  
  <pre><code>Product One
      Child One
      Child Two
  Product Two
      Child One
  </code></pre>
  
  <p>These products are all in one table with a parent id field, the child products have a parent id but the parent items can have a null parent (indicating that product is a top level product)</p>
  
  <p><img src="https://i.stack.imgur.com/7OIig.png" alt="Sample table"></p>
  
  <p>I was thinking something like the following: </p>
  
  <pre><code>var list = GetProductList();
  var newList = new List&lt;ProductDTO&gt;();
  
  var parents = from p in list
      where p.Parent == null
      select p.Id;
  
  
  foreach (var parent in parents)
  {
      var tempList = new List&lt;ProductDTO&gt;();
      tempList.Add(list.FirstOrDefault(x =&gt; x.Id == parent));
      tempList.AddRange(list.Where(x =&gt; x.Parent == parent).OrderBy(x =&gt; x.Id));
      newList.AddRange(tempList);
  }
  </code></pre>
  
  <p>Any suggestions on how I would do this a little cleaner?</p>