DataTable的Compute方法和Expression语法


DataTable的Compute的功能可谓强大。

public object Compute(
 string expression,
 string filter
);

expression:要执行计算的表达式

filter:过滤表达式

这两个表达式都是用Sql语法,例如:

table.Compute("sum(count)", "count>0");

当我们的table中有count和price列时,如果要计算总金额,应该如何写表达式呢?

table.Compute("sum(count*price)", "");

你肯定会想到这样计算,但这样是错误的。我们应该使用表达式,先添加每一行的金额列,然后再计算总金额:

table.Columns.Add("total", typeof(decimal), "count*price");
table.Compute("total", "");

这个时侯才能得出总的金额。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM