linq-to-sqllistsqlcommandsqldbtype

Convert SqlCommand Output to List<MyType>?


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 <MyType>. What is the best way to convert the result set (whether DataTable from a DataAdapter or DataReader bits) into List<MyType>?


Solution

  • You can use LINQ with a DataReader:

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