How to use the Compute method with DataTable or DataSet in C#

Posted By  RRaveen On 26 Jul 2010 04:07:46
emailbookmarkadd commentsprint
No of Views:1551
Bookmarked:0 times
Votes:0 times

Introduction

There 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.

Scenario

I 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.

Implementation

protected 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 Filters

Some more applicable filters in this method
•    Count(*)
•    (Salary + Bonus)
•    AVG(Salary)

I hope this is help to all.

Sign Up to vote for this article
Other popular Tips/Tricks
    In this tip, I will explain how to use the BigInteger in C# 4.0.The BigInteger is new data type in .NET 4.0.
    Published Date : 22/Sep/2011
    In this tips,I am going to discuss about two important thing about Split function of String class. Split function of the string class split the string in array of string.
    Published Date : 29/Jul/2011
    The Facebook is very popular and it has great support to other development Languages by Facebook SDKs.In this tips, I going to explain how to upload Videos to Facebook through C# with few lines of code.
    Published Date : 13/Jun/2011
    In this tip, i will share with you , how to format time HH:MM:SS from seconds in C#.
    Published Date : 25/Feb/2011
    Enums in dot net programming is a great facility and we all used it to increase code readability. In earlier version of .NET framework we don’t have any method anything that will check whether a value is assigned to it or not. In C# 4.0 we have new static method called HasFlag which will check that particular value is assigned or not.
    Published Date : 01/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top