Collect

Projects each element of a sequence to an IEnumerable<TResult> and flattens the resulting sequences into one collection.

Parameters

Returns

Func<T, IEnumerable<TResult>> mapping

IEnumerable<T> source

IEnumerable<TResult>

Usage

A new collection is created by applying the mapping function in each element. After that, the result is flattened.

This function works like the SelectMany method.

Creating a new function

//IEnumerable<int> source = { 1, 2, 3 }

IEnumerable<int> GenerateNumbers(int value)
{
    for(int index = 1; index <= 3; index++)
        yield return value * 10;
}

IEnumerable<int> result = source.Collect(GenerateNumbers);

//result = = { 10, 20, 30, 20, 40, 60, 30, 60, 90 }

Last updated