.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

Session.ExecuteScalarAsync(String, CancellationToken) Method

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.v21.1.dll


public Task<object> ExecuteScalarAsync(
    string sql,
    CancellationToken cancellationToken = default(CancellationToken)


Name Type Description
sql String

Specifies an SQL statement.

Optional Parameters

Name Type Default Description
cancellationToken CancellationToken


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


Type Description

A Task<TResult> that returns the first column of the first row in the query’s result set. A null reference (Nothing in Visual Basic) if the result set does not contain columns. DBNull.Value if the first column does not contain a value.


Use ExecuteScalarAsync to 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 GetOrdersCountAsync(cts.Token);
// ...
public async Task<int> GetOrdersCountAsync(CancellationToken cancellationToken) {
    const string queryString = 
        "SELECT COUNT(*) FROM [Northwind].[dbo].[Orders]";
    object result = await session.ExecuteScalarAsync(
    return (int)result;

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