7

我想要一种优雅的方式来获取像这样的 DataTable:

在此处输入图像描述

并将其变成:

List<Dictionary<string,string>> values = dataTable.ToDictionary();

列表中的每个字典对应一行。字典包含行的值,其中键是列名,值是列值。

该方法应支持动态列数和名称。

4

1 回答 1

15

您需要将每一行变成字典:

// Iterate through the rows...
table.AsEnumerable().Select(
    // ...then iterate through the columns...
    row => table.Columns.Cast<DataColumn>().ToDictionary(
        // ...and find the key value pairs for the dictionary
        column => column.ColumnName,    // Key
        column => row[column] as string // Value
    )
)
于 2013-11-13T16:18:15.763 回答