我需要遍历字典列表
List<Dictionary<string,string>>
填充数据表。列表中的每个字典都有一个键,它需要是列名,以及一个值,它是该列中的内容。该列表包含 225 个字典(表中的 225 行)。
List<Dictionary<string, string>> myList =
JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonRep);
DataTable dt = new DataTable();
//loop through list, loop through dictionaries, add keys as columns,
//values as rows.
到目前为止,我一直在尝试..
//get max columns
int columns = myList[0].Count; <--gives me 13
//add columns
for (int i = 0; i < columns; i++)
dt.Columns.Add(string myList[i].Keys); <--somehow get to the key in dict to add as column names
//add rows
foreach (var x in myList)
{
dt.Rows.Add(x); <--not working
}
jsonReprValue = dt; <--save new DataTable to var jsonReprValue
我该如何正确地做到这一点?谢谢!