title: Dictionary<string, object> prepare using flat list with parent child hierarchy
body: <p>I've a flat list of records from SQL stored procedure which has columns <strong>ID, Key, Value, ParentId.</strong></p>
  
  <p>A key may have any number of child level items or a key may have a direct string value (like a menu). So I defined my dictionary as key and object (object may be a string or again a <code>Dictionary&lt;string, object&gt;</code>).</p>
  
  <p>I need to prepare the dictionary of objects because the key values are dynamic from the database and I need to convert it to json.  So <code>IEnumerable&lt;objects&gt;</code> is not possible. So I am using <code>Dictionary&lt;string, object&gt;</code>.</p>
  
  <p>The problem is it has n levels of hierarchy. So on each recursive call I may need something like yield and return as we do in the Enumerable case but on this Dictionary I need to add the child levels to its immediate parent key.  I'm not sure on how to handle that using Dictionaries.</p>
  
  <p>Here is my code. I think, I am missing something.</p>
  
  <pre><code>private Dictionary&lt;string, object&gt; headerFooterValues;
  private List&lt;S_Get_HeaderFooter_Result&gt; headerFooterResult = default(List&lt;S_Get_HeaderFooter_Result&gt;);
  
  public bool PrepareTree(List&lt;S_Get_HeaderFooter_Result&gt; headerFooters, Dictionary&lt;string, object&gt; childItems, string parentKey = null)
  {
      bool status = false;
      Dictionary&lt;string, object&gt; childKeyValuePairs = new Dictionary&lt;string, object&gt;();
      try
      {
          foreach (S_Get_HeaderFooter_Result item in headerFooters)
          {
              /// always check the whole list
              bool hasChildren = headerFooterResult.Any(row =&gt; item.EntityTypeId == row.ParentId);
  
              /// has children object collection
              if (hasChildren)
              {
                  if (!string.IsNullOrEmpty(parentKey))
                  {
                      childKeyValuePairs = (Dictionary&lt;string, object&gt;)headerFooterValues[parentKey];
                      childKeyValuePairs.Add(item.Key, new Dictionary&lt;string, object&gt;());
                      headerFooterValues[parentKey] = childKeyValuePairs;
                  }
                  else
                  {
                      headerFooterValues.Add(item.Key, new Dictionary&lt;string, object&gt;());
                  }
                  status = PrepareTree(headerFooterResult.Where(row =&gt; item.EntityTypeId == row.ParentId).ToList(), childKeyValuePairs, item.Key);
              }
              /// has no children
              if (!string.IsNullOrEmpty(parentKey)) //if parent key exists prepare child dictionary items
              {
                  childKeyValuePairs = (Dictionary&lt;string, object&gt;)headerFooterValues[parentKey];
                  childKeyValuePairs.Add(item.Key, item.Value);
              }
              else if (!headerFooterValues.ContainsKey(item.Key)) //else prepare parentroot dictionary items
              {
                  headerFooterValues.Add(item.Key, item.Value);
              }
          }
          if (!string.IsNullOrEmpty(parentKey))
          {
              headerFooterValues[parentKey] = childKeyValuePairs;
          }
          status = true;
      }
      catch (Exception)
      {
          status = false;
          throw;
      }
  
      return status;
  }
  </code></pre>