.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+
A newer version of this page is available. Switch to the current version.

Session.ExecuteScalarAsync(String, String[], Object[], CancellationToken) Method

Asynchronously executes the specified SQL query and returns the first column of the first row in the result set returned by the query.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v19.2.dll


public Task<object> ExecuteScalarAsync(
    string sql,
    string[] parameterNames,
    object[] parameterValues,
    CancellationToken cancellationToken = default(CancellationToken)
Public Function ExecuteScalarAsync(
    sql As String,
    parameterNames As String(),
    parameterValues As Object(),
    cancellationToken As CancellationToken = Nothing
) As Task(Of Object)


Name Type Description
sql String

A String value that specifies a parameterized SQL query.

parameterNames String[]

An array of strings specifying names of the parameterValues parameters.

parameterValues Object[]

An array of objects specifying parameters to pass to the database server along with the query.

Optional Parameters

Name Type Default Description
cancellationToken CancellationToken


A CancellationToken object that delivers a cancellation notice to the running operation.


Type Description

The first column of the first row in the query's result set. If the column is not found in the result set, a null reference (Nothing in Visual Basic) is returned. If the value in the database is null, DBNull.Value is returned.


Use ExecuteScalarAsync to asynchronously query data stores for a single or aggregate value, without having to retrieve a result set.

The following example demonstrates how to use this method. Here, session is the Session instance.

using System.Threading;
using System.Threading.Tasks;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
// ...
CancellationTokenSource cts = new CancellationTokenSource();
int ordersCount = await GetEmployeeOrdersCountAsync(cts.Token, 152);
// ...
public async Task<int> GetEmployeeOrdersCountAsync(CancellationToken cancellationToken, 
int employeeId) {
    const string queryString = 
        "SELECT COUNT(*) FROM [Northwind].[dbo].[Orders] WHERE [EmployeeID] = @p1";
    object result = await session.ExecuteScalarAsync(
        new string[] { "p1" }, 
        new object[] { employeeId },
    return (int)result;

Note that a format of parameter names in a SQL statement depends on the ADO.NET provider and database you use.

To learn more about executing SQL queries in XPO, refer to Direct SQL Queries.


The ExecuteScalarAsync method sends queries directly, so the correct query syntax depends on a particular database server.

See Also