IntroductionThere are lots of features in .NET framework. In this article, you will learn another feature is Compute Method. The Compute method, which is exists within the data table object. In this tips, I have taken a simple scenario to use the computer method for DataSet. ScenarioI have collection of employee records with salary and bonus. Now I want to calculate the total of the salary paid a particular month and all sum of the paid salary. Compute//// Summary:// Computes the given expression on the current rows that pass the filter criteria.//// Parameters:// expression:// The expression to compute.//// filter:// The filter to limit the rows that evaluate in the expression.//// Returns:// An System.Object, set to the result of the computation.public object Compute(string expression, string filter); The Compute method given expression on the current rows that pass the filter criteria. And this method accepts the two arguments always. 1. Expression: The expression to compute. 2. Filter: The filter to limit the rows that evaluate in the expression. Implementationprotected void btn_Click(object sender, EventArgs e){// get the colection of employeesDataSet data = GetEmployees();// get sum without any filterobject totalSalary = data.Tables[0].Compute("SUM(Salary)", string.Empty);// get sum of for the current monthobject monthSalary = data.Tables[0].Compute("SUM(Salary)", "DateofPaid > 06/01/2010 AND DateofPaid < 06/30/2010");lblgtotal.Text = "Grand total :" + totalSalary.ToString();lblmtotal.Text = "Month total :" + monthSalary.ToString();}private DataSet GetEmployees(){// here code for the select records from the database}More FiltersSome more applicable filters in this method • Count(*) • (Salary + Bonus) • AVG(Salary) I hope this is help to all. |