我们有 myobj 是这样的
public class MyObj
{
public string Name { get; set; }
public int ID { get; set; }
}
再数据库查询后的datatable我们想把它转成List<object>
我们可以这样做
private List<MyObj> test(DataTable dt)
{
var convertedList = (from rw in dt.AsEnumerable()
select new MyObj()
{
ID = Convert.ToInt32(rw["ID"]),
Name = Convert.ToString(rw["Name"])
}).ToList();
return convertedList;
}
如果不确定object的类型,那我们可以这样做
private List<object> GetListByDataTable(DataTable dt)
{
var reult = (from rw in dt.AsEnumerable()
select new
{
Name = Convert.ToString(rw["Name"]),
ID = Convert.ToInt32(rw["ID"])
}).ToList();
return reult.ConvertAll<object>(o => (object)o);
}