SqlDataReader - How to convert the current row to a dictionary

Martin picture Martin · May 26, 2009 · Viewed 27.7k times · Source

Is there an easy way to convert all the columns of the current row of a SqlDataReader to a dictionary?

using (SqlDataReader opReader = command.ExecuteReader())
{
// Convert the current row to a dictionary
}

Thanks

Answer

SLaks picture SLaks · Nov 26, 2010

You can use LINQ:

return Enumerable.Range(0, reader.FieldCount)
                 .ToDictionary(reader.GetName, reader.GetValue);