Search code examples
linqdictionarydatareader

convert dataReader to Dictionary


I tried to use LINQ to convert one row to Dictionary (fieldName -> fieldValue)

return Enumerable.Range(0, reader.FieldCount)
                 .ToDictionary<string, object>(reader.GetName, reader.GetValue);

but I received error message:

Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<int>' to 'System.Collections.Generic.IEnumerable<string>'

How to correct this?


Solution

  • return Enumerable.Range(0, reader.FieldCount)
                     .ToDictionary(
                         i => reader.GetName(i),
                         i => reader.GetValue(i));