Created
November 22, 2019 02:39
-
-
Save nyinyithann/e1466132480318ec9924e06cbaea7b01 to your computer and use it in GitHub Desktop.
DataTable To ExpandoObjects 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍