Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataTable.Compute Method

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

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

public Object Compute (
	string expression,
	string filter
)
public Object Compute (
	String expression, 
	String filter
)
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.

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 DataColumn.Expression property.

The following example sums the values of a column named "Total," for the salesperson whose identification number is five.

private void ComputeBySalesSalesID(DataSet dataSet)
{
    // Presumes a DataTable named "Orders" that has a column named "Total."
    DataTable table;
    table = dataSet.Tables["Orders"];

    // Declare an object variable.
    object sumObject;
    sumObject = table.Compute("Sum(Total)", "EmpID = 5");


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft