Class SelectManyTransformer<TSource, TDestination>
- Namespace
- Wolfgang.Etl.Transformers
- Assembly
- Wolfgang.Etl.Transformers.dll
A transformer that projects each input item to a sequence of zero or more output items and yields the concatenation of all those sequences.
public sealed class SelectManyTransformer<TSource, TDestination> : ITransformAsync<TSource, TDestination> where TSource : notnull where TDestination : notnull
Type Parameters
TSourceThe type of items in the input sequence. Must be non-null.
TDestinationThe type of items produced by the selector. Must be non-null.
- Inheritance
-
SelectManyTransformer<TSource, TDestination>
- Implements
-
ITransformAsync<TSource, TDestination>
- Inherited Members
- Extension Methods
Examples
// synchronous fan-out: each Order yields all of its OrderLines
var lines = new SelectManyTransformer<Order, OrderLine>(o => o.Lines);
// asynchronous fan-out: each customer id yields a paged stream of related orders
var orders = new SelectManyTransformer<int, Order>
(
id => orderService.StreamForCustomerAsync(id)
);
Remarks
SelectManyTransformer<TSource, TDestination> is the transformer equivalent of LINQ's SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TResult>>): it flattens a one-to-many projection into a single output stream.
Two constructors are provided:
- A synchronous selector returning IEnumerable<T> for in-memory expansions.
- An asynchronous selector returning IAsyncEnumerable<T> for streamed expansions such as a paged database query or a chunked HTTP response.
Output is emitted depth-first: all items produced by the selector for the first input item are yielded before any items produced for the second input item, and so on.
This type implements only Wolfgang.Etl.Abstractions.ITransformAsync<TSource, TDestination> - no progress, no cancellation, no Skip/Max - to keep the hot loop minimal. Compose with dedicated transformers when those concerns are needed.
Exceptions thrown by the selector or by enumerating the returned sequence propagate to the caller. If the selector returns null a NullReferenceException will be raised on iteration, matching the behaviour of LINQ's SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TResult>>).
Constructors
SelectManyTransformer(Func<TSource, IAsyncEnumerable<TDestination>>)
Initializes a new instance with an asynchronous selector that returns an IAsyncEnumerable<T> for each input item.
public SelectManyTransformer(Func<TSource, IAsyncEnumerable<TDestination>> selector)
Parameters
selectorFunc<TSource, IAsyncEnumerable<TDestination>>A function that maps each input item to an asynchronous sequence of zero or more output items. Useful for streamed inner expansions such as paged database queries.
Exceptions
- ArgumentNullException
selectoris null.
SelectManyTransformer(Func<TSource, IEnumerable<TDestination>>)
Initializes a new instance with a synchronous selector that returns an IEnumerable<T> for each input item.
public SelectManyTransformer(Func<TSource, IEnumerable<TDestination>> selector)
Parameters
selectorFunc<TSource, IEnumerable<TDestination>>A function that maps each input item to zero or more output items.
Exceptions
- ArgumentNullException
selectoris null.
Methods
TransformAsync(IAsyncEnumerable<TSource>)
Asynchronously yields the concatenation of the per-item sequences produced by the configured selector.
public IAsyncEnumerable<TDestination> TransformAsync(IAsyncEnumerable<TSource> items)
Parameters
itemsIAsyncEnumerable<TSource>The asynchronous source sequence.
Returns
- IAsyncEnumerable<TDestination>
An asynchronous flattened sequence of items produced by the selector.
Exceptions
- ArgumentNullException
itemsis null.