Transform a DataTable into Dictionary C#

前端 未结 8 1876
爱一瞬间的悲伤
爱一瞬间的悲伤 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:53

    I found the solution but don't know why. I edited my Question completing the code just for make it clear what I was doing an I changed to this

        internal Dictionary GetDict(DataTable dt)
        {
            Dictionary dic = dt.AsEnumerable().ToDictionary(row => row.Field(0), row => row.Field(1));
            return dic;
        }
    
        

    提交回复
    热议问题