C# DataTable按指定列进行求和,查询DataTable指定列为数组或list对象

1、C# DataTable按指定列进行求和

     DataTable dt = MyTeamDAL.Team_GainMoney("", out msg);
//求列G_Money的总和
  decimal totalMoney = Convert.ToDecimal(dt.Compute("sum(G_Money)", ""));

2、DataTable查询出指定列为List对象或数组

 

  DataTable dt = MyTeamDAL.Team_GainMoney("", out msg);    
//将表dt中的所有id转换为long数组
List list_IDS = dt.AsEnumerable().Select(d => d.Field("ID")).ToList();
//将list对象转换为以逗号分隔的字符串
  string str_ids = string.Join(",", list_IDS);

 

你可能感兴趣的:(C#)