(原创)如何合计DataTable的数据

我们可能不想通过sql语句来合计一些字段
通过DataTable的Compute方法一样可以完成这个功能
(数据结构同http://www.cnblogs.com/lovecherry/archive/2005/03/25/125487.html
)
比如添加一个Label1,让这个Label的值等于年龄大于20岁的人的平均年龄
可以这么写
this.Label1.Text=ds.Tables["table1"].Compute("avg(iAge)","iAge>20").ToString();
Compute()方法第一个参数是 聚合函数 (比如sum,avg之类)第二个参数是条件
上述代码基本等于select avg(iAge) from tb1 where iAge>20

原文地址:https://www.cnblogs.com/lovecherry/p/125511.html