1.根据列类型以及列名,取出dataTable某列所有的值
//验证通过
DataTable dt = Pub.mDB_MES.ExcuteForTable(querySQL); // (from d in dt.AsEnumerable() select d.Field<int>("ID")).ToList(); 参考语句 if (dt != null && dt.Rows.Count != 0) { var dtKhList = (from d in dt.AsEnumerable() select d.Field<string>("khid")).ToList();
//或者
dt.AsEnumerable().Select(a => a.Field<string>("khid")).ToList();
}
//没验证
List ls = new List (); //存放一整列所有的值 DataTable dt ; //DataTable foreach (DataRow dr in dt.Rows) { ls.Add(dr["列名"]); }
2.根据每行循环遍历
1.
//通过
(from list in dt.AsEnumerable() select list).ToList().ForEach(rows => { var test = rows["id"].ToString(); });
2.
//通过
List<xx> lists = new List<xx>(); for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr = dt.Rows[i]; xx list= new xx(); xx.id = dr["id"].ToString(); lists .Add(list); }
标签:ToList,Rows,AsEnumerable,List,获取,dt,DataTable From: https://www.cnblogs.com/lixia0604/p/17984952