Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Simple IDataReader Extension Method

5.00/5 (2 votes)
11 Feb 2014CPOL2 min read 15.5K  
A simple, but powerful way to convert an IDataReader to an IEnumerable

Introduction

One of the best features of the .NET framework is its ability to define custom extension methods. This tip will demonstrate a very simple extension method that allows converting an IDataReader into an IEnumerable which then allows querying using LINQ, either through extension methods or the LINQ DSL in C# or VB.NET.

The Extension Method

The extension method is very simple and is as follows:

C#
public IEnumerable<IDataRecord> AsEnumerable(this IDataReader reader)
{
  while(reader.Read())
    yield return reader;
}  

This method is quite simplistic, and yet provides an abundance of features. It leverages C#'s ability to create an enumerable based on the yield return statement and provides the ability to leverage LINQ for various types of queries.

Examples

Using the extension method is equally simple, but powerful.

Example 1 - Convert Reader to an IEnumerable<T>

Let's say, for example, that we want to convert the IDataReader into Person objects.

C#
//Converts reader to person list
public List<Person> GetPersonsFromReader(IDataReader reader)
{
  return reader
    .AsEnumerable()
    .Select(r => new Person { FirstName = (string)r["FirstName"], 
    	LastName = (string)r["LastName"] })
    .ToList();
}   

Notice how simple it is now to covert an IDataReader into an IEnumerable<T> and transform that IDataReader into a meaningful data type. 

Example 2 - Deciding What Data To Return

It's possible that every row from an IDataReader is not what we want. Since we can now convert an IDataReader into a queryable form, we can now exclude rows that do not match the criteria for which we are looking and only include those that do match.

C#
public List<Widget> FindWidgets(IDataReader reader)
{
  return reader
    .AsEnumerable()
    .Where(record => (int)record[0] != 10)
    .Select(record => new Widget { V1 = (int)record[0], 
           V2 = (string)record[1], V3 = (string)record[2] })
    .ToList();
}

Conclusion

I hope this article has been helpful and will be useful. As you can see, it's quite easy to allow any IDataReader, including those of your own making, to be converted into something you can query with all the power of LINQ.

These examples are contrived and may not be proper for your programming project in that I used magic numbers and other programming faux pas that you might want to avoid. However, the extension method itself is quite useful and a welcome addition to any programmer's toolbox or bag of tricks.

I would appreciate any feedback you might have on this tip.

Points of Interest

For more information and details on extension methods, visit MSDN.

If you need more help with the yield return statement, you can also visit the MSDN page on it.

History

  • Version 1.0
  • Version 1.1 - Changed Enumerable<T> to IEnumerable<T>. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)