Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
DataTable Class
DataTable Methods
 Compute Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
Public Function Compute ( _
    expression As String, _
    filter As String _
) As Object
Visual Basic (Usage)
Dim instance As DataTable
Dim expression As String
Dim filter As String
Dim returnValue As Object

returnValue = instance.Compute(expression, _
    filter)
C#
public Object Compute(
    string expression,
    string filter
)
Visual C++
public:
Object^ Compute(
    String^ expression, 
    String^ filter
)
JScript
public function Compute(
    expression : String, 
    filter : String
) : Object

Parameters

expression
Type: System..::.String
The expression to compute.
filter
Type: System..::.String
The filter to limit the rows that evaluate in the expression.

Return Value

Type: System..::.Object
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 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.

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

Visual Basic
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

C#
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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Can return DBNull.Value      mabster   |   Edit   |   Show History

It's worth noting that this method returns DBNull.Value rather than null if no rows match the filter, at least for MIN/MAX expressions.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker