获取DataTable某一列的所有值

 
/// <summary>
/// 获取某一列的所有值
/// </summary>
/// <typeparam name="T">列数据类型</typeparam>
/// <param name="dtSource">数据表</param>
/// <param name="filedName">列名</param>
/// <returns></returns>
public static List<T> GetColumnValues<T>(DataTable dtSource,string filedName)
{
  return (from r in dtSource.AsEnumerable() select r.Field<T>("ID")).ToList<T>();
}

 

方法调用:

获取字段ID的所有值

DataTable dt = ......;

List<int> ids= DataTablePro.GetColumnValues<int>(dt, "ID");

 

直接使用:

//遍历DataTable,取出所有的ID :
List<int> lstID = (from d in dt.AsEnumerable() select d.Field<int>("ID")).ToList();

 

 
posted @ 2020-07-24 18:35  八英里  阅读(1402)  评论(0编辑  收藏  举报