34

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

Martin
  • 39,309
  • 62
  • 192
  • 278

5 Answers5

77

You can use LINQ:

return Enumerable.Range(0, reader.FieldCount)
                 .ToDictionary(reader.GetName, reader.GetValue);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
28

Easier than this?:

// Need to read the row in, usually in a while ( opReader.Read ) {} loop...
opReader.Read();

// Convert current row into a dictionary
Dictionary<string, object> dict = new Dictionary<string, object>();
for( int lp = 0 ; lp < opReader.FieldCount ; lp++ ) {
    dict.Add(opReader.GetName(lp), opReader.GetValue(lp));
}

I'm still not sure why you would need this particular transformation from one type of collection to another.

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
  • 5
    As an answer to your question: if one needs to cache the results, it seems wiser to cache the dictionary copy. Otherwise you end up with a lot of open readers – Toad Mar 02 '10 at 14:18
  • 2
    @Toad One can use a DataTable if you want to cache the results. `MyDataTable.Load(dr);` – Cade Roux Feb 16 '12 at 19:32
  • @CadeRoux a DataTable would read the entire result set instead of a single row no? – Drew R Jul 01 '15 at 23:17
  • 1
    @CadeRoux Saving to a Dictionary is like 4-5 times faster than a DataTable. One test: http://lauteikkehn.blogspot.ro/2012/03/datatable-vs-list.html – Valentin Mar 07 '16 at 12:10
  • 3
    I don't understand why, instead of simply answering, people inquire "why are you doing this? or why are you asking this?" We do not know what the actual scenario for which somebody asks the question – Simple Fellow Apr 11 '16 at 08:45
6

I came across this question on 3/9/2016 and ended up using the answer provided by SLaks. However, I needed to slightly modify it to:

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

I found guidance from this StackOverflow question: convert dataReader to Dictionary

Community
  • 1
  • 1
Mifo
  • 563
  • 6
  • 6
  • This is great, exactly what I was trying to do. It also converts nulls to empty strings. – Dymas Apr 29 '21 at 03:25
0

GetValues method accepts & puts in, all the values in a 1D array.
Does that help?

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
0

It's already an IDataRecord.

That should give you just about the same access (by key) as a dictionary. Since rows don't typically have more than a few handfuls of columns, the performance of the lookups shouldn't be that different. The only important difference is the type of the "payload", and even there your dictionary would have to use object for the value type, so I give the edge to IDataRecord.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 7
    This is useful when you'd like the result of a query to be available after you've freed up the connection. DataReader objects are intrinsically tied to the connection they were created with. – sholsinger Oct 06 '10 at 14:37