DataTable.Compute(String, String) 方法

定义

计算用来传递筛选条件的当前行上的给定表达式。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);
member this.Compute : string * string -> obj
Public Function Compute (expression As String, filter As String) As Object

参数

expression
String

要计算的表达式。The expression to compute.

filter
String

用于限制表达式中计算的行的筛选器。The filter to limit the rows that evaluate in the expression.

返回

Object 设置为计算结果。An Object, set to the result of the computation. 如果该表达式计算结果为 null,则返回值为 ValueIf the expression evaluates to null, the return value will be Value.

示例

下面的示例将名为 "Total" 的列的值与标识号为5的销售人员的值求和。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

注解

expression 参数需要聚合函数。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)

如果必须对两个或更多个列执行操作,则应创建一个 DataColumn,将其 Expression 属性设置为相应的表达式,并对生成的列使用聚合表达式。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. 在这种情况下,假定名称为 "total" 的 DataColumn,并将 Expression 属性设置为:In that case, given a DataColumn with the name "total", and the Expression property set to this:

"Quantity * UnitPrice"

然后,Compute 方法的 expression 参数将如下所示:The expression argument for the Compute method would then be this:

Sum(total)

第二个参数 filter确定在表达式中使用哪些行。The second parameter, filter, determines which rows are used in the expression. 例如,如果表中包含名为 "colDate" 的日期列,则可以使用以下表达式限制行: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

有关为这两个参数创建表达式的规则,请参阅 DataColumn.Expression 属性。For rules on creating expressions for both parameters, see the DataColumn.Expression property.

适用于

另请参阅