title: Order menu nodes by parent, previous and next id
body: <p>I have a small menu which I would like to order by the DB table id like the following:</p>
  
  <pre><code>  public class Obj
      {
          public string Value { get; set; }
          public int? ParentNodeId { get; set; }
          public int? PreviousNodeId { get; set; }
          public int? NextNodeId { get; set; }
      }
  
   private IEnumerable&lt;MenuNodeDTO&gt; GetSortedMenuNodes(int menuId)
      {
          var nodes = LoadMenuNodes(menuId);
          foreach (MenuNode menuNode in nodes
              .Where(s =&gt; s.MenuItemId == null &amp;&amp; s.ParentNodeId == null)
              .OrderBy(x =&gt; x?.PreviousNodeId)
              .Where(x =&gt; x.PreviousNodeId != x.Id))
          {
              MenuNodeDTO tmpMenuNode = new MenuNodeDTO();
              tmpMenuNode.MenuNodeDtoId = menuNode.Id;
              tmpMenuNode.MenuItemCode = menuNode.MenuItem?.Code;
              tmpMenuNode.ChildMenuNodes = GetChildNodes(menuNode).ToList();
              tmpMenuNode.MenuSettings = GetMenuSettings(menuNode).ToList();
              yield return tmpMenuNode;
          }
  }
  </code></pre>
  
  <p><strong>Example:</strong> </p>
  
  <pre><code>Id = 1 MainMenuPoint1 ParentNodeId = null, PreviousNodeId = null, NextNodeId = 4
  Id = 2 -&gt; 1 ChildNodeFromPoint1 ParentNodeId = 1, PreviousNodeId = null, NextNodeId = null
  Id = 3  --&gt; ChildNodeFromFirstChildNode1 ParentNodeId = 2, PreviousNodeId = null, NextNodeId = null
  Id = 4 MainMenuPoint2 ParentNodeId = null, PreviousNodeId = 1, NextNodeId = null
  </code></pre>
  
  <p>The problem with my for each is that when the id changed the ordering not works.
  Has someone a idea how to order by parentid, previousid and next id which check every time all ids for the relationship? </p>