title: How to transform list of hierarchyid into a binary tree
body: <p>I am working on a multi-level marketing (binary) which looks like this:</p>
  
  <p>(but the binary tree is not required to be perfect. A node can have 0-2 child)</p>
  
  <p><a href="https://i.stack.imgur.com/RcB6Y.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RcB6Y.jpg" alt="enter image description here"></a></p>
  
  <p>My problem is the data that I fetch from the database is flat list.
  <a href="https://i.stack.imgur.com/3ouGq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3ouGq.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/zUjbH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zUjbH.jpg" alt="enter image description here"></a></p>
  
  <p>Notice that I am using <a href="https://msdn.microsoft.com/en-us/library/bb677290.aspx" rel="nofollow noreferrer">hierarchyid</a> (sql server 2014)</p>
  
  <p>Basically the <code>TextNode</code> column is like a breadcrumb.</p>
  
  <p>every slash <code>/</code> represents a <code>level</code>.</p>
  
  <p>If I have TextNode of <code>/1/</code> as root. then every node that starts with <code>/1/</code> belongs to that root which are <code>/1/</code>, <code>/1/1/</code> and <code>/1/1/1/</code> (the root node is included which will be the level 0)</p>
  
  <p>I've tried the <a href="https://stackoverflow.com/questions/6945216/converting-flattened-hierarchical-data-from-sql-server-into-a-structured-json-ob">accepted answer</a> in this question but its not working.</p>
  
  <p>How can I transform the flatlist to a Binary Tree so that I can easily traverse and display it on a screen?</p>
  
  <p>Im using C#, ASP MVC 5, SQL Server 2014 if it matters.</p>