我想将数据表的内容复制到列表
List<FireFighterChiefDetails> ChiefList1 = new List<FireFighterChiefDetails>();
cmd = new SqlCommand("select * from FireFighterDetails");
DataTable dt = new DataTable();
dt = Connect.getDataTable(cmd);
我希望将dt的内容复制到ChiefList1
答案 0 :(得分:4)
使用Linq到DataSet:
List<FireFighterChiefDetails> ChiefList1 =
dt.AsEnumerable()
.Select(r => new FireFighterChiefDetails {
Foo = r.Field<string>("Foo"),
Bar = r.Field<int>("Bar")
// etc
}).ToList();
很高兴阅读:Queries in LINQ to DataSet
还考虑使用一些(迷你)ORM(如Dapper)将查询结果自动映射到您的班级:
string sql = "select * from FireFighterDetails";
var ChiefList1 = connection.Query<FireFighterChiefDetails>(sql).ToList();
答案 1 :(得分:-1)
这样的事情怎么样:
List<object> obj = new List<object>();
foreach (DataRow dr in dt.Rows)
{
obj.Add(dr);
}