This question already has an answer here:
I have data in a DataReader
which I want to be converted to a List<T>
.
What is a possible simple solution for this?
For e.g. in CustomerEntity class, I have CustomerId and CustomerName properties.If my DataReader returns these two columns as data, then how can I convert it into List<CustomerEntity>
.
I know this question is old, and already answered, but...
Since SqlDataReader already implements IEnumerable, why is there a need to create a loop over the records?
I've been using the method below without any issues, nor without any performance issues: So far I have tested with IList, List(Of T), IEnumerable, IEnumerable(Of T), IQueryable, and IQueryable(Of T)
Strong Typing Class
Usage
I have written the following method using this case.
First, add the namespace:
System.Reflection
For Example:
T
is return type(ClassName) anddr
is parameter to mappingDataReader
C#, Call mapping method like the following:
This is the mapping method:
VB.NET, Call mapping method like the following:
This is the mapping method:
You cant simply (directly) convert the datareader to list.
You have to loop through all the elements in datareader and insert into list
below the sample code