Convert SqlCommand Output to List<MyType>?

Snowy picture Snowy · May 17, 2011 · Viewed 9.7k times · Source

I am using an ADO.NET SqlCommand with a single SqlDbType.Structured parameter to send a table-valued parameter to a sproc. The sproc returns many rows, which I need to get into a strongly-Typed List of . What is the best way to convert the result set (whether DataTable from a DataAdapter or DataReader bits) into List?

Thanks.

Answer

SLaks picture SLaks · May 17, 2011

You can use LINQ with a DataReader:

var list = reader.Cast<IDataRecord>()
                 .Select(dr => new YourType { Name = dr.GetString(0), ... })
                 .ToList();