Table to List<object> C#

We have myobj like this
public class MyObj { public string Name { get; set; } public int ID { get; set; } }

And then query the database datatable after we want it to turn into a List <object>

We can do this

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; }

If unsure of the type of object that we can do this

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); }

Guess you like

Origin www.cnblogs.com/z45281625/p/12076426.html