title: Dictionary<string, object> to DataTable
body: <p>I have a very strange issue, and no clue which way I should take to fix it.</p>
  
  <p>I have an <code>IEnumerable&lt;Dictionary&lt;string,object&gt;&gt;</code> and it can contain one or more <code>IEnumerable&lt;Dictionary&lt;string,object&gt;&gt;</code>. </p>
  
  <p>Now, this Dictionary needs to be imported into DataTable, and if the <code>IEnumberable&lt;Dictionary&lt;string,object&gt;&gt;</code> inside has 0 children, then the DataTable should contain only one row with the Column names as strings, and the RowData as objects (string in this case). But, if there is a child, then the DataTable should contains the same number of rows as this child, and other information in every row from parent.</p>
  
  <p>For instance, the parent Dictionary has these values:</p>
  
  <pre>
  string, object
  ---------------
  Name, Mike
  LastName, Tyson
  </pre>
  
  <p>IEnumerable Dictionary child has:</p>
  
  <pre>
  string, object
  ----------------
  [0]
  ChildName, John
  ChildAge, 10
  
  [1]
  ChildName, Tony
  ChildAge, 12
  </pre>
  
  <p>Result should be:</p>
  
  <pre>
  Name    LastName     ChildName     ChildAge
  --------------------------------------------
  Mike    Tyson        John          10
  Mike    Tyson        Tony          12
  </pre>
  
  <p>Also, Parent IEnumerable can have many children IEnumerable, but they will all have the same size.</p>
  
  <p>Does anyone have idea how to solve this?</p>
  
  <pre><code>static void Main(string[] args)
  {
      var child1 = new List&lt;Dictionary&lt;string, object&gt;&gt;();
      var childOneDic = new Dictionary&lt;string, object&gt; 
      { 
          { "ChildName", "John" }, 
          { "ChildAge", 10 } 
      };
      child1.Add(childOneDic);
  
      var child2 = new List&lt;Dictionary&lt;string, object&gt;&gt;();
      var childTwoDic = new Dictionary&lt;string, object&gt; 
      { 
          { "ChildName", "Tony" }, 
          { "ChildAge", 12 } 
      };
      child2.Add(childTwoDic);
  
      var parrent = new List&lt;Dictionary&lt;string, object&gt;&gt;();
      var parrentDic = new Dictionary&lt;string, object&gt;
      {
          { "Name", "Mike" },
          { "LastName", "Tyson" },
          { "child1", child1 },
          { "child2", child2 }
      };
      parrent.Add(parrentDic);
  
      var table = new DataTable();
      table.Columns.Add("Name");
      table.Columns.Add("LastName");
      table.Columns.Add("ChildName");
      table.Columns.Add("ChildAge");
      table = CreateTable(parrent, null, table);
  }
  
  static DataTable CreateTable(IEnumerable&lt;Dictionary&lt;string, object&gt;&gt; parrent, 
                               DataRow row, DataTable table)
  {
      if (row == null)
      {
          row = table.NewRow();
      }
  
      foreach (var v in parrent)
      {
          foreach (var o in v)
          {
              if (o.Value.GetType().IsGenericType)
              {
                  var dic = (IEnumerable&lt;Dictionary&lt;string, object&gt;&gt;) o.Value;
                  CreateTable(dic, row, table);
              }
              else
              {
                  row[o.Key] = o.Value;
              }
          }
          if (row.RowState == DataRowState.Added)
          {
              DataRow tempRow = table.NewRow();
              tempRow.ItemArray = row.ItemArray;
              table.Rows.Add(tempRow);
          }
          else
          {
              table.Rows.Add(row);
          }
      }
  
      return table;
  }
  </code></pre>