Table of Contents

Method IterateWhileAsync

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

IterateWhileAsync(DbDataReader, Func<IDataRecord, bool>, CancellationToken)

Asynchronously iterates the remaining records of the current result set of a DbDataReader while a predicate returns true; the reader's connection is held while each row is processed.

public static ValueTask IterateWhileAsync(this DbDataReader reader, Func<IDataRecord, bool> predicate, CancellationToken cancellationToken = default)

Parameters

reader DbDataReader

The IDataReader to iterate.

predicate Func<IDataRecord, bool>

The function to call for each IDataRecord. It returns true to continue with the next record, or false to stop.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default is None.

Returns

ValueTask

A task that represents the asynchronous iteration.

Remarks

caution

This is not the recommended way to read rows: it holds the connection while your code runs for each row. The reader, and the connection under it, stay open for as long as your code takes per row, added up over all the rows.

In async code that time has no bound: every await in the loop body can yield, and nothing says when the loop resumes. Until it does, the connection is on hold: a pooled connection is not returned to the pool, and the server may keep its locks and the open result set.

These members suit work that must happen while the connection is open, for example a result too large to buffer or a row-by-row pipeline. Otherwise use a call that lets go of the connection first: ToListAsync(CancellationToken) and ToArrayAsync(CancellationToken) buffer the rows and release the connection (call .Map<T>() first), and ToPooledStream(CancellationToken) streams them to a consumer without holding it (the producer is unbounded: it reads without waiting for the consumer and frees the connection while the consumer takes its time, and the rows wait in memory until the consumer takes them; dispose the enumerator, as await foreach does).

The same holds when you pass in a reader you opened yourself: the connection under it is held.

The token is passed to ReadAsync, so the provider cancels the read itself. A cancel throws, and rows already handed out stay handed out. A cancel during the predicate of a row throws at the next read, even if that row was the last. Once a read has returned no row the result is complete, and a cancel after that is not an error. To stop early, return false from the predicate: that ends the iteration without an exception.

Exceptions

ArgumentNullException

reader or predicate is null.

OperationCanceledException

The token was canceled.

IterateWhileAsync(DbDataReader, Func<IDataRecord, bool>, bool, CancellationToken)

Asynchronously iterates the remaining records of the current result set of a DbDataReader while a predicate returns true; the reader's connection is held while each row is processed.

public static ValueTask IterateWhileAsync(this DbDataReader reader, Func<IDataRecord, bool> predicate, bool useReadAsync, CancellationToken cancellationToken = default)

Parameters

reader DbDataReader

The DbDataReader to iterate.

predicate Func<IDataRecord, bool>

The function to call for each IDataRecord. It returns true to continue with the next record, or false to stop.

useReadAsync bool

true to read the rows with ReadAsync, so the provider can cancel a read itself; false to read them with Read, checking the token before each read.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default is None.

Returns

ValueTask

A task that represents the asynchronous iteration.

Remarks

caution

This is not the recommended way to read rows: it holds the connection while your code runs for each row. The reader, and the connection under it, stay open for as long as your code takes per row, added up over all the rows.

In async code that time has no bound: every await in the loop body can yield, and nothing says when the loop resumes. Until it does, the connection is on hold: a pooled connection is not returned to the pool, and the server may keep its locks and the open result set.

These members suit work that must happen while the connection is open, for example a result too large to buffer or a row-by-row pipeline. Otherwise use a call that lets go of the connection first: ToListAsync(CancellationToken) and ToArrayAsync(CancellationToken) buffer the rows and release the connection (call .Map<T>() first), and ToPooledStream(CancellationToken) streams them to a consumer without holding it (the producer is unbounded: it reads without waiting for the consumer and frees the connection while the consumer takes its time, and the rows wait in memory until the consumer takes them; dispose the enumerator, as await foreach does).

The same holds when you pass in a reader you opened yourself: the connection under it is held.

The token is passed to ReadAsync, so the provider cancels the read itself. When useReadAsync is false it is checked before each Read instead. A cancel throws, and rows already handed out stay handed out. A cancel during the predicate of a row throws at the next read, even if that row was the last. Once a read has returned no row the result is complete, and a cancel after that is not an error. To stop early, return false from the predicate: that ends the iteration without an exception.

Exceptions

ArgumentNullException

reader or predicate is null.

OperationCanceledException

The token was canceled.

IterateWhileAsync(IDataReader, Func<IDataRecord, ValueTask<bool>>, CancellationToken)

Asynchronously iterates the remaining records of the current result set of an IDataReader while an asynchronous predicate returns true; the reader's connection is held while each row is processed.

public static ValueTask IterateWhileAsync(this IDataReader reader, Func<IDataRecord, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)

Parameters

reader IDataReader

The IDataReader to iterate.

predicate Func<IDataRecord, ValueTask<bool>>

The function to call for each IDataRecord. It returns true to continue with the next record, or false to stop.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default is None.

Returns

ValueTask

A task that represents the asynchronous iteration.

Remarks

caution

This is not the recommended way to read rows: it holds the connection while your code runs for each row. The reader, and the connection under it, stay open for as long as your code takes per row, added up over all the rows.

In async code that time has no bound: every await in the loop body can yield, and nothing says when the loop resumes. Until it does, the connection is on hold: a pooled connection is not returned to the pool, and the server may keep its locks and the open result set.

These members suit work that must happen while the connection is open, for example a result too large to buffer or a row-by-row pipeline. Otherwise use a call that lets go of the connection first: ToListAsync(CancellationToken) and ToArrayAsync(CancellationToken) buffer the rows and release the connection (call .Map<T>() first), and ToPooledStream(CancellationToken) streams them to a consumer without holding it (the producer is unbounded: it reads without waiting for the consumer and frees the connection while the consumer takes its time, and the rows wait in memory until the consumer takes them; dispose the enumerator, as await foreach does).

The same holds when you pass in a reader you opened yourself: the connection under it is held.

A DbDataReader is read with ReadAsync, so the provider cancels the read itself; any other reader is checked before each Read, and a cancel throws before the read is made. A cancel throws, and rows already handed out stay handed out. A cancel during the predicate of a row throws at the next read, even if that row was the last. Once a read has returned no row the result is complete, and a cancel after that is not an error. To stop early, return false from the predicate: that ends the iteration without an exception.

Exceptions

ArgumentNullException

reader or predicate is null.

OperationCanceledException

The token was canceled.

IterateWhileAsync(IDataReader, Func<IDataRecord, ValueTask<bool>>, bool, CancellationToken)

Asynchronously iterates the remaining records of the current result set of an IDataReader while an asynchronous predicate returns true; the reader's connection is held while each row is processed.

public static ValueTask IterateWhileAsync(this IDataReader reader, Func<IDataRecord, ValueTask<bool>> predicate, bool useReadAsync, CancellationToken cancellationToken = default)

Parameters

reader IDataReader

The IDataReader to iterate.

predicate Func<IDataRecord, ValueTask<bool>>

The asynchronous function to call for each IDataRecord. It returns true to continue with the next record, or false to stop.

useReadAsync bool

true to read the rows with ReadAsync, so the provider can cancel a read itself (a reader that is not a DbDataReader is always read with Read); false to read them with Read, checking the token before each read.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default is None.

Returns

ValueTask

A task that represents the asynchronous iteration.

Remarks

caution

This is not the recommended way to read rows: it holds the connection while your code runs for each row. The reader, and the connection under it, stay open for as long as your code takes per row, added up over all the rows.

In async code that time has no bound: every await in the loop body can yield, and nothing says when the loop resumes. Until it does, the connection is on hold: a pooled connection is not returned to the pool, and the server may keep its locks and the open result set.

These members suit work that must happen while the connection is open, for example a result too large to buffer or a row-by-row pipeline. Otherwise use a call that lets go of the connection first: ToListAsync(CancellationToken) and ToArrayAsync(CancellationToken) buffer the rows and release the connection (call .Map<T>() first), and ToPooledStream(CancellationToken) streams them to a consumer without holding it (the producer is unbounded: it reads without waiting for the consumer and frees the connection while the consumer takes its time, and the rows wait in memory until the consumer takes them; dispose the enumerator, as await foreach does).

The same holds when you pass in a reader you opened yourself: the connection under it is held.

When useReadAsync is true, a DbDataReader is read with ReadAsync, so the provider cancels the read itself. Otherwise the token is checked before each Read, and a cancel throws before the read is made. A cancel during the predicate of a row throws at the next read, even if that row was the last. Once a read has returned no row the result is complete, and a cancel after that is not an error. To stop early, return false from the predicate: that ends the iteration without an exception.

Exceptions

ArgumentNullException

reader or predicate is null.

OperationCanceledException

The token was canceled.