Table of Contents

Method ExecuteScalarAsync

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.Core.dll

ExecuteScalarAsync(CancellationToken)

Asynchronously executes the command with ExecuteScalarAsync(CancellationToken).

public ValueTask<object?> ExecuteScalarAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token that can cancel the call. It is used together with the command's own token (see UseCancellationToken(CancellationToken)); either one cancels.

Returns

ValueTask<object>

The raw value the provider returns: Value when the column is NULL and null when there is no row, which is how to tell the two apart. Use Open.Database.Extensions.ExpressiveCommandBase<TConnection, TCommand, TReader, TDbType, TThis>.ExecuteScalar<T>() for the value converted, or a transform overload to receive null in both.

ExecuteScalarAsync<T>(Func<object?, T>, CancellationToken)

Asynchronously executes the command with ExecuteScalarAsync(CancellationToken) and passes the value to a transform.

public ValueTask<T> ExecuteScalarAsync<T>(Func<object?, T> transform, CancellationToken cancellationToken = default)

Parameters

transform Func<object, T>

The function that converts the value. It receives null when the column is NULL or there is no row, never DBNull; any other value is passed as the provider returned it.

cancellationToken CancellationToken

A token that can cancel the call. It is used together with the command's own token (see UseCancellationToken(CancellationToken)); either one cancels.

Returns

ValueTask<T>

A task that represents the asynchronous operation; its result is the value returned from the transform.

Type Parameters

T

The type expected.

ExecuteScalarAsync<T>(CancellationToken)

Asynchronously executes the command with ExecuteScalarAsync(CancellationToken) and converts the value to the expected type, as Map<T>() converts a column.

public ValueTask<T> ExecuteScalarAsync<T>(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token that can cancel the call. It is used together with the command's own token (see UseCancellationToken(CancellationToken)); either one cancels.

Returns

ValueTask<T>

The value, converted to T: a value that is already a T is returned as is, an integer is converted to another integer type with a range check (a long read as an int), and any other value takes the general conversion (a decimal read as a long). null when there was no row and T takes null; an error when there was no row and T is a non-nullable value type; null when the result is NULL and T takes it.

Type Parameters

T

The type to convert the value to.

Exceptions

ValueMappingException

The value does not convert to T, or is out of its range (the conversion's own error is the inner exception), there was no row or it is NULL and T is a non-nullable value type.

ValueMappingException

There was no row or the value is NULL and T is a non-nullable value type (ask for the nullable type and use ?? for a default), the value does not convert to T, or it is out of its range (the conversion's own error is the inner exception).

ExecuteScalarAsync<T>(Func<object?, ValueTask<T>>, CancellationToken)

Asynchronously executes the command with ExecuteScalarAsync(CancellationToken) and passes the value to an asynchronous transform.

public ValueTask<T> ExecuteScalarAsync<T>(Func<object?, ValueTask<T>> transform, CancellationToken cancellationToken = default)

Parameters

transform Func<object, ValueTask<T>>

The function (task) that converts the value. It receives null when the column is NULL or there is no row, never DBNull; any other value is passed as the provider returned it.

cancellationToken CancellationToken

A token that can cancel the call. It is used together with the command's own token (see UseCancellationToken(CancellationToken)); either one cancels.

Returns

ValueTask<T>

A task that represents the asynchronous operation; its result is the value returned from the transform.

Type Parameters

T

The type expected.