将DataTable转换为字典<;字符串,字符串>;指定所涉及的列名,而不是使用foreach
本文关键字:字符串 foreach 字典 转换 DataTable lt gt | 更新日期: 2023-09-27 17:59:04
使用循环这样做完全没有问题,但我想知道是否有内置的转换函数,当它们一起使用时,可以将ADO.NET DataTable
(包含两列以上)中的数据移动到一行中的Dictionary<string,string>
,其中带键的列和带值的列是由名称而非索引位置指定的。
尚未对此进行测试,但通过阅读文档:
IEnumerable<Dictionary<string, string>> dic =
dataTable.Rows.OfType<DataRow>()
.Select(row => dataTable.Columns.OfType<DataColumn>()
.ToDictionary(col => col.ColumnName, col => row[col] != null ? row[col].ToString() : null));
您要求一行:)