【问题标题】:Convert DataTable to Dictionary<string,string> specifying the column-names involved, not using foreach将 DataTable 转换为 Dictionary<string,string> 指定所涉及的列名,而不是使用 foreach
【发布时间】:2016-07-27 13:37:16
【问题描述】:

使用循环执行此操作完全没有问题,但我想知道是否有内置的转换函数,当它们一起使用时,可以在 ADO.NET DataTable 中移动数据(包含两列以上) 进入单行中的Dictionary&lt;string,string&gt;,其中带有键的列和带有值的列由名称而不是索引位置指定。

【问题讨论】:

    标签: c# linq dictionary datatable ado.net


    【解决方案1】:

    尚未对此进行测试,但通过阅读文档:

    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));
    

    您要求的是单线 :)

    【讨论】:

    • 是的,我确实要求一个班轮 :) 谢谢。稍后我到办公室时会测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2014-10-22
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多