嘿我有这样的代码
它从读者返回模型列表。我怎样才能做得更好?不要使用这个
address.Add(new Address {
ID = (int) reader["ID"],
Address = reader["Address"].ToString(),
PhoneNumber = (int) reader["PhoneNumber"]
});
但使用更多这样的address.Add(new Address(reader?or somthing else));,或者也许如果我可以不使用列表并返回数据,仅在它返回的类中我可以添加到可观察集合?
public async Task<IEnumerable<Address>> LoadDetail(int id)
{
await conn.OpenAsync();
List<Address> address = new List<Address>();
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText = $"SELECT * FROM Address WHERE ID = {id}";,
using (SqlDataReader reader = command.ExecuteReader())
{
while (await reader.ReadAsync())
{
address.Add(new Address { ID = (int)reader["ID"], Address = reader["Address"].ToString(), PhoneNumber = (int)reader["PhoneNumber"] });
}
}
}
return address;
}
LEATH
相关分类