title: Loading flat data into a tree data structure
body: <p>Currently, I have an Item class and a list of items representing flat data retrieved from a SQL Server 2012 DB query. Here is the Item class.</p>
  
  <pre><code>public class Item
  {
      public string Parent { get; set; }
      public string Child { get; set; }
      public string Name { get; set; }
      public int Quantity { get; set; }
  }
  </code></pre>
  
  <p>I need to transform this flat data into a tree structure and have the ability to traverse the tree while performing some action. Here is a visual of some data. My tree will always have a single root node.</p>
  
  <p><a href="https://i.stack.imgur.com/I8SzD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I8SzD.jpg" alt="Tree Data and Structure"></a></p>
  
  <p>I have seen several posts on SO pertaining to this but I'm still struggling with it. Here is the generic TreeNode class I have.</p>
  
  <pre><code>public class TreeNode&lt;T&gt;
  {
      private readonly T _value;
      private readonly List&lt;TreeNode&lt;T&gt;&gt; _children = new List&lt;TreeNode&lt;T&gt;&gt;();
  
      public TreeNode(T value)
      {
          _value = value;
      }
  
      public TreeNode&lt;T&gt; this[int i]
      {
          get { return _children[i]; }
      }
  
      public TreeNode&lt;T&gt; Parent { get; private set; }
  
      public T Value { get { return _value; } }
  
      public ReadOnlyCollection&lt;TreeNode&lt;T&gt;&gt; Children
      {
          get { return _children.AsReadOnly(); }
      }
  
      public TreeNode&lt;T&gt; AddChild(T value)
      {
          var node = new TreeNode&lt;T&gt;(value) { Parent = this };
          _children.Add(node);
          return node;
      }
  
      public TreeNode&lt;T&gt;[] AddChildren(params T[] values)
      {
          return values.Select(AddChild).ToArray();
      }
  
      public bool RemoveChild(TreeNode&lt;T&gt; node)
      {
          return _children.Remove(node);
      }
  
      public void Traverse(Action&lt;T&gt; action)
      {
          action(Value);
          foreach (var child in _children)
              child.Traverse(action);
      }
  
      public IEnumerable&lt;T&gt; Flatten()
      {
          return new[] { Value }.Concat(_children.SelectMany(x =&gt; x.Flatten()));
      }
  }
  </code></pre>
  
  <p>I'm having trouble recursively loading the flat data into the tree structure. I also don't understand how to utilize the Action inside the Traverse method to perform some task while traversing. To populate the tree, I believe I need to start with the root. I obtain it like this:</p>
  
  <pre><code>var root = new TreeNode&lt;Item&gt;(items.Single(i =&gt; i.Parent == null));
  </code></pre>
  
  <p>I can then load the first level down like so:</p>
  
  <pre><code>root.AddChildren(items.Where(i =&gt; i.Parent.Equals(root.Value.Child)).ToArray());
  </code></pre>
  
  <p>My two questions are as follows:</p>
  
  <ol>
  <li>How can I recursively load everything?</li>
  <li>How do I use the Action inside the Traverse method to simply write out the tree structure by item names with proper indentation (or perform any task for that matter)?</li>
  </ol>
  
  <p>Thanks in advance!!</p>