Transform a DataTable into Dictionary C#

前端 未结 8 1858
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 07:13

I want to know how to transform a DataTable into a Dictionary. I did something like this.

using System.Linq;

internal Dictionary GetDict(Da         


        
8条回答
  •  醉话见心
    2021-02-02 07:26

    i think this will help you:

                DataTable dt = new DataTable();
                dt.Columns.Add("Column1");
                dt.Columns.Add("Column2");
                dt.Rows.Add(1, "first");
                dt.Rows.Add(2, "second");
                var dictionary = dt.Rows.OfType().ToDictionary(d => d.Field(0), v => v.Field(1));
    
        

    提交回复
    热议问题