Skip to content

Instantly share code, notes, and snippets.

@mgrigajtis
Last active March 17, 2024 18:08
Show Gist options
  • Save mgrigajtis/5d5a98d3b5f8800bf462829918197e4c to your computer and use it in GitHub Desktop.
Save mgrigajtis/5d5a98d3b5f8800bf462829918197e4c to your computer and use it in GitHub Desktop.
Convert Data Table to Dictionary in C#
private List<Dictionary<string, object>> DataTableToDictionary(DataTable dt)
{
var dictionaries = new List<Dictionary<string, object>>();
foreach (DataRow row in dt.Rows)
{
Dictionary<string, object> dictionary = Enumerable.Range(0, dt.Columns.Count).ToDictionary(i => dt.Columns[i].ColumnName, i => row.ItemArray[i]);
dictionaries.Add(dictionary);
}
return dictionaries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment