I have a method that returns a data reader. Normally I would wrap a using around the data reader so that it gets disposed nicely after I iterate through it. THe problem is that I've chosen to consume the data reader using Linq, which means that the defered execution causes the reader to get disposed early. Is there a proper pattern to consume and dispose of the data reader using Linq without having to built a complete collection of it's contents?
using (System.Data.SqlClient.SqlDataReader reader = CallStoredProcedure())
{
return reader.Cast<System.Data.Common.DbDataRecord>().Select(rec => new ObjectModel.CollectorSummaryItem()
{
CollectorID = (int)rec[0],
Name = rec.IsDBNull(1) ? null : rec.GetString(1),
Information = rec.IsDBNull(2) ? null : rec.GetString(2)
});
}