DataTable 轉為 List<dynamic>

LowKeyC發表於2024-03-24

static List<dynamic> DataTableToListDynamic(DataTable dt)
{
List<dynamic> dynamicList = new List<dynamic>();

foreach (DataRow row in dt.Rows)
{
dynamic dynamicObj = new ExpandoObject();
var expandoDict = (IDictionary<string, object>)dynamicObj;
foreach (DataColumn column in dt.Columns)
{
expandoDict[column.ColumnName] = row[column];
}
dynamicList.Add(dynamicObj);
}

return dynamicList;
}

static List<dynamic> DataTableToListDynamic(DataTable dt)
{
List<dynamic> dynamicList = new List<dynamic>();

foreach (DataRow row in dt.Rows)
{
dynamic dynamicObj = new ExpandoObject();
var expandoDict = (IDictionary<string, object>)dynamicObj;
foreach (DataColumn column in dt.Columns)
{
expandoDict[column.ColumnName] = row[column];
}
dynamicList.Add(dynamicObj);
}

return dynamicList;
}

相關文章