Software Technology Tips

Compute is one of the methods of DataTable class. This method computes an expression on the current rows that pass the filter criteria.
Object DataTable.Compute(string expression , string filter)
Expression - The expression to compute.
Filter - The filter to limit the rows that evaluate in the expression.
This method's return type is System.Object. So, you have to cast it with appropriate data type.
The following operations can be passed through as expression parameter.
Sum -> Sum
Average -> Avg
Minimum -> Min
Maximum -> Max
Count -> Count
Statistical standard deviation ->StDev
Statistical variance -> Var
DataTable dtTotal = new DataTable();
Suppose your DataTable dtTotal contain two columns as
Name      Price
 x          20
 y          50
 z          30
 u          10
 x          25
 y          15
 x          35
 u          20
If you want to count the total price of distinct names, then you can use compute method like below statement.
int total = dtTotal.Compute("Sum ( Price ) ", "Name = 'x'");
Output - 80
This method looks pretty simple, but sometimes its SQL like Aggregate Function functionality solves complex calculations.

Related Tags:


Author: Priyanka Dash


Let us Connect!

Awards and Achievements

Red Herringcolor DeloitteFast50 DB ZInnov1   Nascome


serf DDM

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login