Possible Duplicate:
How can I easily convert DataReader to List<T>?
I want to return list which will store the sqldatareader
value. how can i do this..
using (var reader = cmd.ExecuteReader())
{
var result = new List<Foo>();
while (reader.Read())
{
var foo = new Foo
{
Prop1 = reader.GetInt32(0),
Prop2 = reader.GetString(1),
}
result.Add(foo);
}
}
Also check this previously asked qestion : How can I easily convert DataReader to List<T>?
Check this for generic type :
public ConvertToList<T>(SqlDataReader sqldr, int index)
{
List<T> list = new List<T>();
while (sqldr.Read()) {
list.Add((T)sqldr.GetValue(index)); index++;
}
return list;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With