DataTable转List<Dictionary<string, object>>的两种方法
方法一、使用linq
List<Dictionary<string, object>> dict = dt.AsEnumerable().Select(
row => dt.Columns.Cast<DataColumn>().ToDictionary(
column => column.ColumnName, // 键
column => row[column] as object // 值
)).ToList();
方法二、使用foreach循环
List<Dictionary<string, object>> dict = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
dic.Add(dc.ColumnName,dr[dc]);
}
dict.Add(dic);
}