Yar*_*evi 4 c# datatable dictionary todictionary
我想要一个优雅的方式来获取像这样的DataTable:

把它变成:
List<Dictionary<string,string>> values = dataTable.ToDictionary();
Run Code Online (Sandbox Code Playgroud)
列表中的每个字典对应一行.字典包含行的值,其中键是列名称,值是列值.
该方法应支持动态列数和名称.
SLa*_*aks 12
您需要将每一行转换为字典:
// 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
)
)
Run Code Online (Sandbox Code Playgroud)