DataTable.Compute(String, String) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Computes the given expression on the current rows that pass the filter criteria.
public:
System::Object ^ Compute(System::String ^ expression, System::String ^ filter);
public object Compute (string? expression, string? filter);
public object Compute (string expression, string filter);
member this.Compute : string * string -> obj
Public Function Compute (expression As String, filter As String) As Object
Parameters
- expression
- String
The expression to compute.
- filter
- String
The filter to limit the rows that evaluate in the expression.
Returns
An Object, set to the result of the computation. If the expression evaluates to null, the return value will be Value.
Examples
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");
}
Private Sub ComputeBySalesSalesID(ByVal dataSet As DataSet)
' Presumes a DataTable named "Orders" that has a column named "Total."
Dim table As DataTable
table = dataSet.Tables("Orders")
' Declare an object variable.
Dim sumObject As Object
sumObject = table.Compute("Sum(Total)", "EmpID = 5")
End Sub
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 this:
"Quantity * UnitPrice"
The expression argument for the Compute method would then be this:
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.