title: How to convert Dictionary<string, List<object>> into DataTable in C#?
body: <p>I have a <code>Dictionary&lt;string, AFValues&gt;</code> which <code>AFValues</code> is a collection of type AFValue object. </p>
  
  <p>A snapshot of what is behind the scene of the dictionary data structure:
  <a href="https://i.stack.imgur.com/B8AhV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B8AhV.png" alt="enter image description here"></a></p>
  
  <p>The <code>Value</code> for each <code>Key</code> has this <code>AFValues</code> collection:
  <a href="https://i.stack.imgur.com/F7EsJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F7EsJ.png" alt="enter image description here"></a></p>
  
  <p>Then, this is what I have written so far to convert the dictionary into a data table.</p>
  
  <pre><code>private DataTable dataTable = null;    
  private DataTable ConvertToDataTable(Dictionary&lt;string, AFValues&gt; dict)
          {
              using (dataTable = new DataTable())
              {
                  if (dict.Count &gt; 0)
                  {
                      var headers = dict.Keys;
                      foreach (var colHeader in headers)
                      {
                          dataTable.Columns.Add(colHeader);
                      }
  
                      foreach (var row in dict)
                      {
                          DataRow dataRow = dataTable.NewRow();
                          foreach (var afVal in row.Value)
                          {
                              dataRow[row.Key] = afVal.Value;
                          }
                          dataTable.Rows.Add(dataRow);
                      }
                  }
              }
              return dataTable;
          }
  </code></pre>
  
  <p>I have successfully created the <code>DataTable</code> columns based on the <code>dict.Keys</code> which is fairly straightforward.  However, my problem lies on how to correctly loop through <code>AFValues</code> and mapped each <code>AFValue</code> to their corresponding column (<code>dict.Key</code>)?</p>
  
  <p>I have done my research, but couldn't find any related scenario which is the same as mine.</p>
  
  <p>This is the returned <code>DataTable</code> based on the code above which is incorrect.</p>
  
  <p><a href="https://i.stack.imgur.com/tCW9w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tCW9w.png" alt="enter image description here"></a></p>
  
  <p>This should be the expected output (sample table)</p>
  
  <p><a href="https://i.stack.imgur.com/CrQ7O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CrQ7O.png" alt="enter image description here"></a></p>
  
  <p>Any help is greatly appreciated.</p>