To calculate the aggregate of a single table column, use the DataTable object’s Compute method. Pass it an expression string that contains an aggregate function with a columnname argument
C# DataTable employees = new DataTable("Employee");
employees.Columns.Add("ID", typeof(int));
employees.Columns.Add("Gender", typeof(string)); employees.Columns.Add("FullName", typeof(string));
employees.Columns.Add("Salary", typeof(decimal));
// ----- Add employee data to table, then... decimal averageSalary=(decimal)employees.Compute("Avg(Salary)", "");
In the preceding code, the Compute method calculates the average of the values in the Salary column. The second argument to Compute is a filter that limits the rows included in the calculation. It accepts a Boolean criteria expression similar to those used in the DataTable. Select method call.
C#
int femalesInCompany = (int)employees.Compute("Count(ID)", "Gender = 'F'");