Transform a DataTable into Dictionary C#

前端 未结 8 1875
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  猫巷女王i
    2021-02-02 07:44

    ToDictionary is expecting the IEnumberable as the first type... you were telling it that it was a string which is wrong it's IEnumerable

    It's getting confused by you specifying the types... try this...

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

    提交回复
    热议问题