Table of Contents

Method Select

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

Select<T>(IDataReader, Func<IDataRecord, T>)

Lazily transforms each remaining record of an IDataReader through a function.

public static IEnumerable<T> Select<T>(this IDataReader reader, Func<IDataRecord, T> transform)

Parameters

reader IDataReader

The IDataReader to iterate.

transform Func<IDataRecord, T>

The function to transform each IDataRecord into a result.

Returns

IEnumerable<T>

An enumerable of the transformed results.

Type Parameters

T

The return type of the transform function.

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 iteration is deferred: nothing is read until the result is enumerated. Rows already handed out stay handed out. To stop early, stop enumerating.

Exceptions

ArgumentNullException

reader or transform is null.

Select<T>(IDataReader, Func<IDataRecord, T>, CancellationToken)

Lazily transforms each remaining record of an IDataReader through a function.

public static IEnumerable<T> Select<T>(this IDataReader reader, Func<IDataRecord, T> transform, CancellationToken cancellationToken)

Parameters

reader IDataReader

The IDataReader to iterate.

transform Func<IDataRecord, T>

The function to transform each IDataRecord into a result.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

IEnumerable<T>

An enumerable of the transformed results.

Type Parameters

T

The return type of the transform function.

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 iteration is deferred: nothing is read, and a canceled token does not throw, until the result is enumerated. The token is checked before each read: a cancel throws from the enumeration, and the read is not made. A cancel while a row is being yielded or transformed lets that row finish, then throws at the next check, 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. Rows already handed out stay handed out. To stop early without an exception, stop enumerating.

Exceptions

ArgumentNullException

reader or transform is null.

OperationCanceledException

The token was canceled (thrown by the enumeration).

Select<T>(IDataReader, CancellationToken, Func<IDataRecord, T>)

Lazily transforms each remaining record of an IDataReader through a function.

public static IEnumerable<T> Select<T>(this IDataReader reader, CancellationToken cancellationToken, Func<IDataRecord, T> transform)

Parameters

reader IDataReader

The IDataReader to iterate.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

transform Func<IDataRecord, T>

The function to transform each IDataRecord into a result.

Returns

IEnumerable<T>

An enumerable of the transformed results.

Type Parameters

T

The return type of the transform function.

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 iteration is deferred: nothing is read, and a canceled token does not throw, until the result is enumerated. The token is checked before each read: a cancel throws from the enumeration, and the read is not made. A cancel while a row is being yielded or transformed lets that row finish, then throws at the next check, 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. Rows already handed out stay handed out. To stop early without an exception, stop enumerating.

Exceptions

ArgumentNullException

reader or transform is null.

OperationCanceledException

The token was canceled (thrown by the enumeration).