Skip to content

Instantly share code, notes, and snippets.

@nyinyithann
Created November 22, 2019 02:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyinyithann/e1466132480318ec9924e06cbaea7b01 to your computer and use it in GitHub Desktop.
Save nyinyithann/e1466132480318ec9924e06cbaea7b01 to your computer and use it in GitHub Desktop.
DataTable To ExpandoObjects C#
public static class DataTableExtensions
{
public static IEnumerable<dynamic> ToExpandoObjectList(this DataTable self)
{
var result = new List<dynamic>(self.Rows.Count);
foreach (var row in self.Rows.OfType<DataRow>())
{
var expando = new ExpandoObject() as IDictionary<string, object>;
foreach (var col in row.Table.Columns.OfType<DataColumn>())
{
expando.Add(col.ColumnName, row[col]);
}
result.Add(expando);
}
return result;
}
}
@hyzx86
Copy link

hyzx86 commented Feb 13, 2023

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment