Transform a DataTable into Dictionary C#

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

    The generic method ToDictionary has 3 parameters. You left one off, so it doesn't know what to do. If you want to specify all of the parameters, it would be .

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

    Of course, if you leave them off, the compiler is able to infer the types, so you don't get the error.

    提交回复
    热议问题