ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

Select

It returns reader lines which can be retrieved from lamba statement

Source

public static IEnumerable<T> Select<T>(this SqlDataReader reader, Func<SqlDataReader, T> projection)
{
   while (reader.Read())
   {
      yield return projection(reader);
   }
}

Example

using (DataReader reader = ...)
{
    List<Customer> customers = reader.Select(r => new Customer {
        CustomerId = r["id"] is DBNull ? null : r["id"].ToString(),
        CustomerName = r["name"] is DBNull ? null : r["name"].ToString() 
    }).ToList();
}

Author: Credit goes to Jon Skeet

Submitted on: 7 mei 2012

Language: C#

Type: System.Data.SqlClient.SqlDataReader

Views: 4118