Last active
March 17, 2024 18:08
-
-
Save mgrigajtis/5d5a98d3b5f8800bf462829918197e4c to your computer and use it in GitHub Desktop.
Convert Data Table to Dictionary in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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