Advertisement
Genral

Convert_Dictionary to DataTable

Apr 27th, 2024
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1. private DataTable ConvertToDataTable(Dictionary<int, Dictionary<string, object>> dictionary)
  2.         {
  3.             DataTable table = new DataTable();
  4.  
  5.             foreach (var innerDictionary in dictionary.Values)
  6.             {
  7.                 if (table.Columns.Count == 0)
  8.                 {
  9.                     foreach (var columnName in innerDictionary.Keys)
  10.                     {
  11.                         table.Columns.Add(columnName);
  12.                     }
  13.                 }
  14.  
  15.                 DataRow row = table.NewRow();
  16.                 foreach (var keyValuePair in innerDictionary)
  17.                 {
  18.                     row[keyValuePair.Key] = keyValuePair.Value;
  19.                 }
  20.                 table.Rows.Add(row);
  21.             }
  22.  
  23.             return table;
  24.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement