thể trùng lặp:
How can I easily convert DataReader to List<T>?Làm thế nào tôi có thể chuyển đổi SqlDataReader List <>
Tôi muốn quay trở lại danh sách đó sẽ lưu trữ các giá trị sqldatareader
. làm thế nào tôi có thể làm điều này ..
thể trùng lặp:
How can I easily convert DataReader to List<T>?Làm thế nào tôi có thể chuyển đổi SqlDataReader List <>
Tôi muốn quay trở lại danh sách đó sẽ lưu trữ các giá trị sqldatareader
. làm thế nào tôi có thể làm điều này ..
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);
}
}
Ngoài ra kiểm tra qestion hỏi trước này: How can I easily convert DataReader to List<T>?
Kiểm tra này cho loại hình chung:
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;
}
Cũ thảo luận, nhưng cố gắng này trên cho kích thước.
Nhập mạnh các trường của bạn và truyền các hàng của bạn vào đối tượng DbDataRecord. ví dụ:
Dim _Query = (From row In _Results.Cast(Of DbDataRecord)().AsParallel()
Select New uTyping.Profile() With {
.Id = row(0),
.Firstname = row(6)
}).ToList()
tôi thích điều này, nhưng thích 'result.Add (new Foo() {Prop1 = reader.GetInt32 (0), Prop2 = reader.GetString (1)}); ' – Justin