How to Generate a Single Aggregate?

Answer

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'");

All ado.net Questions

Ask your interview questions on ado-net

Write Your comment or Questions if you want the answers on ado-net from ado-net Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---