Export (0) Print
Expand All

DataTable.Compute Method

Computes the given expression on the current rows that pass the filter criteria.

[Visual Basic]
Public Function Compute( _
   ByVal expression As String, _
   ByVal filter As String _
) As Object
[C#]
public object Compute(
 string expression,
 string filter
);
[C++]
public: Object* Compute(
 String* expression,
 String* filter
);
[JScript]
public function Compute(
   expression : String,
 filter : String
) : Object;

Parameters

expression
The expression to compute.
filter
The filter to limit the rows that evaluate in the expression.

Return Value

An Object, set to the result of the computation.

Remarks

The expression parameter requires an aggregate function. For example, the following is a legal expression:

Count(Quantity)

But this expression is not:

Sum (Quantity * UnitPrice)

If you must perform an operation on two or more columns, you should create a DataColumn, set its Expression property to an appropriate expression, and use an aggregate expression on the resulting column. In that case, given a DataColumn with the name "total," and the Expression property set to:

"Quantity * UnitPrice"

The expression argument for the Compute method would then be:

Sum(total)

The second parameter filter determines which rows are used in the expression. For example, if the table contains a date column named "colDate", you could limit the rows with the following expression:

colDate > 1/1/99 AND colDate < 17/1/99

For rules on creating expressions for both parameters, see the Expression property of the DataColumn class.

Example

[Visual Basic, C#, C++] The following example sums the values of a column named "Total," for the saleperson whose identification number is 5.

[Visual Basic] 
Private Sub ComputeBySalesSalesID(ByVal myDataSet As DataSet)
    ' Presumes a DataTable named "Orders" that has a column named "Total."
    Dim myTable As DataTable
    myTable = myDataSet.Tables("Orders")
    ' Declare an object variable.
    Dim objSum As Object
    objSum = myTable.Compute("Sum(Total)", "EmpID = 5")
 End Sub

[C#] 
private void ComputeBySalesSalesID(DataSet myDataSet){
    // Presumes a DataTable named "Orders" that has a column named "Total."
    DataTable myTable;
    myTable = myDataSet.Tables["Orders"];
    // Declare an object variable.
    object objSum;
    objSum = myTable.Compute("Sum(Total)", "EmpID = 5");
 }

[C++] 
private:
void ComputeBySalesSalesID(DataSet* myDataSet){
    // Presumes a DataTable named "Orders" that has a column named "Total."
    DataTable* myTable;
    myTable = myDataSet->Tables->Item[S"Orders"];
    // Declare an object variable.
    Object* objSum;
    objSum = myTable->Compute(S"Sum(Total)", S"EmpID = 5");
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTable Class | DataTable Members | System.Data Namespace | Expression

Show:
© 2015 Microsoft