2 Replies - 2399 Views - Last Post: 23 September 2011 - 06:25 AM

#1 Byrdsong  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 194
  • Joined: 16-November 08

Extension Method SqlDataReader.ToList

Posted 16 September 2011 - 01:40 PM

Description: Initialize a List<T> and store results of a SqlDataReader by calling "ToList<T>()". Make sure property names of "T" match column names of data source.Converts a sql data reader result set to a list of T objects

using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Data;

public static List<T> ToList<T>(this SqlDataReader reader) where T : new()
        {
            //create a list to hold our values
            var list = new List<T>();

            //if the reader is dead throw exception
            if (reader == null)
                throw new NullReferenceException("The value of 'null' was found where the instance of an object was required.");

            //if reader has row values
            if (reader.HasRows)
            {
                //grab columnCount
                var columnCount = reader.FieldCount;

                //loop thru reader
                while (reader.Read())
                {
                    //create new item
                    var item = new T();

                    //iterate thru column values per row
                    for (int i = 0; i < columnCount; i++)
                    {
                        //get columnName to match property name of our T item
                        var columnName = reader.GetName(i);
                        var itemProperty = item.GetType().GetProperty(columnName);

                        //if no property name found, move on to the next column
                        if (itemProperty == null) continue;
                        
                        //column name match, set its value
                        itemProperty.SetValue(item, reader[i], null);
                    }

                    //after population, add item
                    list.Add(item);
                }

                //close reader
                reader.Close();
            }

            //return populated list
            return list;
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Extension Method SqlDataReader.ToList

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Extension Method SqlDataReader.ToList

Posted 19 September 2011 - 08:25 AM

The only thing I suggest is changing the return type of the method to IList. Other than that, great snippet. One other thing that must be made clear: this method must be wrapped in a static class to work.
Was This Post Helpful? 0
  • +
  • -

#3 Byrdsong  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 194
  • Joined: 16-November 08

Re: Extension Method SqlDataReader.ToList

Posted 23 September 2011 - 06:25 AM

Good eye. That's why it's always best to let a 2nd pair of eyes review code. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1