Session.ExecuteScalarAsync(String, Object[], 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.v24.2.dll
NuGet Package: DevExpress.Xpo
#Declaration
public Task<object> ExecuteScalarAsync(
string sql,
object[] parameterValues,
CancellationToken cancellationToken = default(CancellationToken)
)
#Parameters
Name | Type | Description |
---|---|---|
sql | String | Specifies an SQL statement. |
parameter |
Object[] | An array of objects specifying parameters to pass to the database server along with the query. |
#Optional Parameters
Name | Type | Default | Description |
---|---|---|---|
cancellation |
Cancellation |
null | A Cancellation |
#Returns
Type | Description |
---|---|
Task<Object> | A Task |
#Remarks
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 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(
queryString,
new object[] { employeeId },
cancellationToken
);
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.
Note
The Execute