Transform a DataTable into Dictionary C#

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 <DataRow, string, object>.

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

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

Leave a Comment

tech