How do you convert a DataTable into a generic list?

前端 未结 27 3000
后悔当初
后悔当初 2020-11-22 17:04

Currently, I\'m using:

DataTable dt = CreateDataTableInSomeWay();

List list = new List(); 
foreach (DataRow dr in dt.Rows)
{
          


        
27条回答
  •  无人共我
    2020-11-22 17:36

    To assign the DataTable rows to the generic List of class

      List temp = new List();//List that holds the Candidate Class,
        //Note:The Candidate class contains RollNo,Name and Department
        //tb is DataTable
        temp = (from DataRow dr in tb.Rows
                            select new Candidate()
                            {
                                RollNO = Convert.ToInt32(dr["RollNO"]),
                                Name = dr["Name"].ToString(),
                                Department = dr["Department"].ToString(),
    
                            }).ToList();
    

提交回复
热议问题