Class QueryCommandExtensions
Extension methods that run a QueryCommand<TResult> against raw SQL instead of the generated query, and the terminal operators that stream LOB columns through SequentialAccess: the single-column ToStream(QueryCommand<byte[]>, params ReadOnlySpan<object?>) / ToTextReader(QueryCommand<string>, params ReadOnlySpan<object?>) and the multi-column ToDataReader<TResult>(QueryCommand<TResult>, params ReadOnlySpan<object?>), with their asynchronous counterparts.
public static class QueryCommandExtensions
- Inheritance
-
QueryCommandExtensions
- Inherited Members
Methods
PrepareFromSql<TResult>(QueryCommand<TResult>, string, PrepareFromSqlMode, CancellationToken)
Prepares the command to read the given SQL using the given caching/streaming mode.
public static IPreparedQueryCommand<TResult> PrepareFromSql<TResult>(this QueryCommand<TResult> queryCommand, string sql, PrepareFromSqlMode mode, CancellationToken cancellationToken = default)
Parameters
queryCommandQueryCommand<TResult>The command to run.
sqlstringThe SQL text to execute.
modePrepareFromSqlModeHow the prepared command is cached and whether it streams.
cancellationTokenCancellationTokenA token that cancels preparation.
Returns
- IPreparedQueryCommand<TResult>
The prepared command.
Type Parameters
TResultThe projected result type.
PrepareFromSql<TResult>(QueryCommand<TResult>, string, object?, PrepareFromSqlMode, CancellationToken)
Prepares the command to read the given SQL with parameters and an explicit caching/streaming mode.
public static IPreparedQueryCommand<TResult> PrepareFromSql<TResult>(this QueryCommand<TResult> queryCommand, string sql, object? @params, PrepareFromSqlMode mode, CancellationToken cancellationToken = default)
Parameters
queryCommandQueryCommand<TResult>The command to run.
sqlstringThe SQL text to execute.
paramsobjectAn object whose public properties become the SQL parameters, or
null.modePrepareFromSqlModeHow the prepared command is cached and whether it streams.
cancellationTokenCancellationTokenA token that cancels preparation.
Returns
- IPreparedQueryCommand<TResult>
The prepared command.
Type Parameters
TResultThe projected result type.
PrepareFromSql<TResult>(QueryCommand<TResult>, string, object?, CancellationToken)
Prepares the command to read the given SQL with parameters supplied as the public properties of params.
public static IPreparedQueryCommand<TResult> PrepareFromSql<TResult>(this QueryCommand<TResult> queryCommand, string sql, object? @params, CancellationToken cancellationToken = default)
Parameters
queryCommandQueryCommand<TResult>The command to run.
sqlstringThe SQL text to execute.
paramsobjectAn object whose public properties become the SQL parameters, or
null.cancellationTokenCancellationTokenA token that cancels preparation.
Returns
- IPreparedQueryCommand<TResult>
The prepared command.
Type Parameters
TResultThe projected result type.
PrepareFromSql<TResult>(QueryCommand<TResult>, string, CancellationToken)
Prepares the command to read the given SQL, materializing the result as TResult.
public static IPreparedQueryCommand<TResult> PrepareFromSql<TResult>(this QueryCommand<TResult> queryCommand, string sql, CancellationToken cancellationToken = default)
Parameters
queryCommandQueryCommand<TResult>The command to run.
sqlstringThe SQL text to execute.
cancellationTokenCancellationTokenA token that cancels preparation.
Returns
- IPreparedQueryCommand<TResult>
The prepared command.
Type Parameters
TResultThe projected result type.
ToDataReaderAsync<TResult>(QueryCommand<TResult>, params object?[])
Asynchronously opens a forward-only DbDataReader over the command's multi-column projection.
public static Task<DbDataReader> ToDataReaderAsync<TResult>(this QueryCommand<TResult> command, params object?[] parameters)
Parameters
commandQueryCommand<TResult>The command to execute.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task<DbDataReader>
A task producing a reader that owns the provider reader and the per-call command until it is disposed.
Type Parameters
TResultThe projected result type; it is not materialized on this path.
Remarks
The caller owns the returned reader and must dispose it, which releases the provider reader and the
per-call command; the context and its connection stay open. On PostgreSQL and SQL Server the reader
uses sequential access: read columns in ascending ordinal order and do not read a column twice. On
SQLite the projection is locator-free and buffered (non-sequential): FieldCount equals the
projection column count and a large BLOB is read whole rather than chunked. The terminal fails
closed on providers without sequential access (MySQL/MariaDB, ClickHouse) and on the in-memory
provider, which has no DbDataReader.
ToDataReaderAsync<TResult>(QueryCommand<TResult>, CancellationToken, params object?[])
Asynchronously opens a forward-only DbDataReader over the command's multi-column projection.
public static Task<DbDataReader> ToDataReaderAsync<TResult>(this QueryCommand<TResult> command, CancellationToken cancellationToken, params object?[] parameters)
Parameters
commandQueryCommand<TResult>The command to execute.
cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task<DbDataReader>
A task producing a reader that owns the provider reader and the per-call command until it is disposed.
Type Parameters
TResultThe projected result type; it is not materialized on this path.
Remarks
The caller owns the returned reader and must dispose it, which releases the provider reader and the
per-call command; the context and its connection stay open. On PostgreSQL and SQL Server the reader
uses sequential access: read columns in ascending ordinal order and do not read a column twice. On
SQLite the projection is locator-free and buffered (non-sequential): FieldCount equals the
projection column count and a large BLOB is read whole rather than chunked. The terminal fails
closed on providers without sequential access (MySQL/MariaDB, ClickHouse) and on the in-memory
provider, which has no DbDataReader.
ToDataReader<TResult>(QueryCommand<TResult>, params ReadOnlySpan<object?>)
Opens a forward-only DbDataReader over the command's multi-column projection.
public static DbDataReader ToDataReader<TResult>(this QueryCommand<TResult> command, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<TResult>The command to execute.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- DbDataReader
A reader that owns the provider reader and the per-call command until it is disposed.
Type Parameters
TResultThe projected result type; it is not materialized on this path.
Remarks
The caller owns the returned reader and must dispose it, which releases the provider reader and the
per-call command; the context and its connection stay open. On PostgreSQL and SQL Server the reader
uses sequential access: read columns in ascending ordinal order and do not read a column twice. On
SQLite the projection is locator-free and buffered (non-sequential): FieldCount equals the
projection column count and a large BLOB is read whole rather than chunked. The terminal fails
closed on providers without sequential access (MySQL/MariaDB, ClickHouse) and on the in-memory
provider, which has no DbDataReader.
ToDataReader<TResult>(QueryCommand<TResult>, CancellationToken, params ReadOnlySpan<object?>)
Opens a forward-only DbDataReader over the command's multi-column projection.
public static DbDataReader ToDataReader<TResult>(this QueryCommand<TResult> command, CancellationToken cancellationToken, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<TResult>The command to execute.
cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- DbDataReader
A reader that owns the provider reader and the per-call command until it is disposed.
Type Parameters
TResultThe projected result type; it is not materialized on this path.
Remarks
The caller owns the returned reader and must dispose it, which releases the provider reader and the
per-call command; the context and its connection stay open. On PostgreSQL and SQL Server the reader
uses sequential access: read columns in ascending ordinal order and do not read a column twice. On
SQLite the projection is locator-free and buffered (non-sequential): FieldCount equals the
projection column count and a large BLOB is read whole rather than chunked. The terminal fails
closed on providers without sequential access (MySQL/MariaDB, ClickHouse) and on the in-memory
provider, which has no DbDataReader.
ToStream(QueryCommand<byte[]>, params ReadOnlySpan<object?>)
Opens a streaming read of a single byte[] column.
public static Stream ToStream(this QueryCommand<byte[]> command, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<byte[]>The command whose projection is exactly one
byte[]column.parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- Stream
A stream that owns the reader until it is disposed.
ToStream(QueryCommand<byte[]>, CancellationToken, params ReadOnlySpan<object?>)
Opens a streaming read of a single byte[] column.
public static Stream ToStream(this QueryCommand<byte[]> command, CancellationToken cancellationToken, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<byte[]>The command whose projection is exactly one
byte[]column.cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- Stream
A stream that owns the reader until it is disposed.
Remarks
On the in-memory provider the value is already materialized: the returned read-only MemoryStream wraps it, and a missing row or a NULL value yields Null.
ToStream(QueryCommand<Stream>, params ReadOnlySpan<object?>)
Opens a streaming read of a binary column selected by name as a Stream.
public static Stream ToStream(this QueryCommand<Stream> command, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<Stream>The command whose projection is a single streaming binary column.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- Stream
A stream that owns the reader and the per-call command until it is disposed.
ToStream(QueryCommand<Stream>, CancellationToken, params ReadOnlySpan<object?>)
Opens a streaming read of a binary column selected by name as a Stream.
public static Stream ToStream(this QueryCommand<Stream> command, CancellationToken cancellationToken, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<Stream>The command whose projection is a single streaming binary column.
cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- Stream
A stream that owns the reader and the per-call command until it is disposed.
ToStreamAsync(QueryCommand<byte[]>, params object?[])
Asynchronously opens a streaming read of a single byte[] column.
public static Task<Stream> ToStreamAsync(this QueryCommand<byte[]> command, params object?[] parameters)
Parameters
commandQueryCommand<byte[]>The command whose projection is exactly one
byte[]column.parametersobject[]The positional parameter values bound to the query.
Returns
ToStreamAsync(QueryCommand<byte[]>, CancellationToken, params object?[])
Asynchronously opens a streaming read of a single byte[] column.
public static Task<Stream> ToStreamAsync(this QueryCommand<byte[]> command, CancellationToken cancellationToken, params object?[] parameters)
Parameters
commandQueryCommand<byte[]>The command whose projection is exactly one
byte[]column.cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersobject[]The positional parameter values bound to the query.
Returns
Remarks
On the in-memory provider the value is already materialized: the returned read-only MemoryStream wraps it, and a missing row or a NULL value yields Null.
ToStreamAsync(QueryCommand<Stream>, params object?[])
Asynchronously opens a streaming read of a binary column selected by name as a Stream.
public static Task<Stream> ToStreamAsync(this QueryCommand<Stream> command, params object?[] parameters)
Parameters
commandQueryCommand<Stream>The command whose projection is a single streaming binary column.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task<Stream>
A task producing a stream that owns the reader and the per-call command until it is disposed.
ToStreamAsync(QueryCommand<Stream>, CancellationToken, params object?[])
Asynchronously opens a streaming read of a binary column selected by name as a Stream.
public static Task<Stream> ToStreamAsync(this QueryCommand<Stream> command, CancellationToken cancellationToken, params object?[] parameters)
Parameters
commandQueryCommand<Stream>The command whose projection is a single streaming binary column.
cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task<Stream>
A task producing a stream that owns the reader and the per-call command until it is disposed.
ToTextReader(QueryCommand<TextReader>, params ReadOnlySpan<object?>)
Opens a streaming read of a text column selected by name as a TextReader.
public static TextReader ToTextReader(this QueryCommand<TextReader> command, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<TextReader>The command whose projection is a single streaming text column.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- TextReader
A text reader that owns the reader and the per-call command until it is disposed.
ToTextReader(QueryCommand<TextReader>, CancellationToken, params ReadOnlySpan<object?>)
Opens a streaming read of a text column selected by name as a TextReader.
public static TextReader ToTextReader(this QueryCommand<TextReader> command, CancellationToken cancellationToken, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<TextReader>The command whose projection is a single streaming text column.
cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- TextReader
A text reader that owns the reader and the per-call command until it is disposed.
ToTextReader(QueryCommand<string>, params ReadOnlySpan<object?>)
Opens a streaming read of a single string column.
public static TextReader ToTextReader(this QueryCommand<string> command, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<string>The command whose projection is exactly one
stringcolumn.parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- TextReader
A text reader that owns the reader until it is disposed.
ToTextReader(QueryCommand<string>, CancellationToken, params ReadOnlySpan<object?>)
Opens a streaming read of a single string column.
public static TextReader ToTextReader(this QueryCommand<string> command, CancellationToken cancellationToken, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<string>The command whose projection is exactly one
stringcolumn.cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Returns
- TextReader
A text reader that owns the reader until it is disposed.
Remarks
On the in-memory provider the value is already materialized: the returned StringReader wraps it, and a missing row or a NULL value yields Null.
ToTextReaderAsync(QueryCommand<TextReader>, params object?[])
Asynchronously opens a streaming read of a text column selected by name as a TextReader.
public static Task<TextReader> ToTextReaderAsync(this QueryCommand<TextReader> command, params object?[] parameters)
Parameters
commandQueryCommand<TextReader>The command whose projection is a single streaming text column.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task<TextReader>
A task producing a text reader that owns the reader and the per-call command until it is disposed.
ToTextReaderAsync(QueryCommand<TextReader>, CancellationToken, params object?[])
Asynchronously opens a streaming read of a text column selected by name as a TextReader.
public static Task<TextReader> ToTextReaderAsync(this QueryCommand<TextReader> command, CancellationToken cancellationToken, params object?[] parameters)
Parameters
commandQueryCommand<TextReader>The command whose projection is a single streaming text column.
cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task<TextReader>
A task producing a text reader that owns the reader and the per-call command until it is disposed.
ToTextReaderAsync(QueryCommand<string>, params object?[])
Asynchronously opens a streaming read of a single string column.
public static Task<TextReader> ToTextReaderAsync(this QueryCommand<string> command, params object?[] parameters)
Parameters
commandQueryCommand<string>The command whose projection is exactly one
stringcolumn.parametersobject[]The positional parameter values bound to the query.
Returns
- Task<TextReader>
A task producing a text reader that owns the reader until it is disposed.
ToTextReaderAsync(QueryCommand<string>, CancellationToken, params object?[])
Asynchronously opens a streaming read of a single string column.
public static Task<TextReader> ToTextReaderAsync(this QueryCommand<string> command, CancellationToken cancellationToken, params object?[] parameters)
Parameters
commandQueryCommand<string>The command whose projection is exactly one
stringcolumn.cancellationTokenCancellationTokenA token that cancels opening the reader.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task<TextReader>
A task producing a text reader that owns the reader until it is disposed.
Remarks
On the in-memory provider the value is already materialized: the returned StringReader wraps it, and a missing row or a NULL value yields Null.
WithSql<TResult>(QueryCommand<TResult>, string)
Overrides the command's generated SQL with sql and no parameters.
public static QueryCommand<TResult> WithSql<TResult>(this QueryCommand<TResult> queryCommand, string sql)
Parameters
queryCommandQueryCommand<TResult>The command to override.
sqlstringThe SQL text to execute.
Returns
- QueryCommand<TResult>
The same command, with the raw-SQL override attached.
Type Parameters
TResultThe projected result type.
WithSql<TResult>(QueryCommand<TResult>, string, object?)
Overrides the command's generated SQL with sql, taking the SQL parameters from
the public properties of params.
public static QueryCommand<TResult> WithSql<TResult>(this QueryCommand<TResult> queryCommand, string sql, object? @params)
Parameters
queryCommandQueryCommand<TResult>The command to override.
sqlstringThe SQL text to execute.
paramsobjectAn object whose public properties become the SQL parameters, or
null.
Returns
- QueryCommand<TResult>
The same command, with the raw-SQL override attached.
Type Parameters
TResultThe projected result type.
WriteCsvAsync<TResult>(QueryCommand<TResult>, Stream, CsvStreamOptions?, CancellationToken, params object?[])
Asynchronously writes the command's Select result as CSV to destination.
public static Task WriteCsvAsync<TResult>(this QueryCommand<TResult> command, Stream destination, CsvStreamOptions? options = null, CancellationToken cancellationToken = default, params object?[] parameters)
Parameters
commandQueryCommand<TResult>The command to execute.
destinationStreamThe stream that receives the UTF-8 CSV; it stays open.
optionsCsvStreamOptionsThe CSV dialect options, or
nullfor the defaults.cancellationTokenCancellationTokenA token that cancels the write.
parametersobject[]The positional parameter values bound to the query.
Returns
- Task
A task that completes when the CSV has been written.
Type Parameters
TResultThe projected result type; it is not materialized on this path.
WriteCsv<TResult>(QueryCommand<TResult>, Stream, CsvStreamOptions?, CancellationToken, params ReadOnlySpan<object?>)
Writes the command's Select result as CSV to destination.
public static void WriteCsv<TResult>(this QueryCommand<TResult> command, Stream destination, CsvStreamOptions? options = null, CancellationToken cancellationToken = default, params ReadOnlySpan<object?> parameters)
Parameters
commandQueryCommand<TResult>The command to execute.
destinationStreamThe stream that receives the UTF-8 CSV; it stays open.
optionsCsvStreamOptionsThe CSV dialect options, or
nullfor the defaults.cancellationTokenCancellationTokenA token that cancels the write.
parametersReadOnlySpan<object>The positional parameter values bound to the query.
Type Parameters
TResultThe projected result type; it is not materialized on this path.