键值集合List转换成datatable

/// <summary>
/// 键值集合List转换成datatable
/// </summary>
/// <param name="data">数据源</param>
/// <returns></returns>
DataTable ToDataTable(List<Dictionary<string, string>> data)
{ DataTable dt = new DataTable(); foreach (var item in data[].Keys)
{//循环添加列
dt.Columns.Add(new DataColumn(item));
}
foreach (var item in data)
{//把数据填充到行
DataRow dr = dt.NewRow();
foreach (var ii in item)
{
dr[ii.Key] = ii.Value;
}
//把数据添加到datatable
dt.Rows.Add(dr);
} return dt;
} //调用
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
ToDataTable(data)
上一篇:Django——ModuleNotFoundError: No module named 'asgiref.sync'


下一篇:使用pynlpir增强jieba分词的准确度