Method Choose
| Edit this page View SourceChoose<T, TResult>(IEnumerable<T>, Func<T, (bool, TResult)>)
Applies a function to each element of the source sequence and returns a new sequence of result elements for source elements where the function returns a couple (2-tuple) having a true as its first element and result as the second.
Declaration
public static IEnumerable<TResult> Choose<T, TResult>(this IEnumerable<T> source, Func<T, (bool, TResult)> chooser)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
Func<T, (bool, TResult)> | chooser | The function that is applied to each source element. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in |
TResult | The type of the elements in the returned sequence. |
Remarks
This method uses deferred execution semantics and streams its results.
Examples
The following code example demonstrates choose and project elements in a sequence using the Choose
operator.
var sequence = "O,l,2,3,4,S,6,7,B,9".Split(',');
// Use a function to choose and project elements in the sequence
var result = sequence
.Choose(s => (int.TryParse(s, out var n), n));
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [2, 3, 4, 6, 7, 9]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|