Method WhereLead
| Edit this page View SourceWhereLead<TSource>(IEnumerable<TSource>, int, Func<TSource, TSource?, bool>)
Filters a sequence of values based on a predicate evaluated on the current value and a leading value.
Declaration
public static IEnumerable<TSource> WhereLead<TSource>(this IEnumerable<TSource> source, int offset, Func<TSource, TSource?, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence over which to evaluate Lead. |
int | offset | The offset (expressed as a positive number) by which to lead each element of the sequence. |
Func<TSource, TSource, bool> | predicate | A function which accepts the current and subsequent (lead) element (in that order) to test for a condition. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | An IEnumerable<T> that contains elements from the input sequence that satisfy the condition. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements in the source sequence. |
Remarks
For elements of the sequence that are less than offset
items from the end, default(TSource
?) is used as the lead value.
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to filter a sequence based on a leading value in the sequence using WhereLead
.
var sequence = Enumerable.Range(1, 10);
// Filter a sequence based on a leading value
var result = sequence
.WhereLead(1, (cur, lead) => cur + lead >= 10);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [5, 6, 7, 8, 9, 10]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
WhereLead<TSource>(IEnumerable<TSource>, int, TSource, Func<TSource, TSource, bool>)
Filters a sequence of values based on a predicate evaluated on the current value and a leading value.
Declaration
public static IEnumerable<TSource> WhereLead<TSource>(this IEnumerable<TSource> source, int offset, TSource defaultLeadValue, Func<TSource, TSource, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence over which to evaluate Lead. |
int | offset | The offset (expressed as a positive number) by which to lead each element of the sequence. |
TSource | defaultLeadValue | A default value supplied for the leading element when none is available |
Func<TSource, TSource, bool> | predicate | A function which accepts the current and subsequent (lead) element (in that order) to test for a condition. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | An IEnumerable<T> that contains elements from the input sequence that satisfy the condition. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements in the source sequence. |
Remarks
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to filter a sequence based on a leading value in the sequence using WhereLead
.
var sequence = Enumerable.Range(1, 10);
// Filter a sequence based on a leading value
var result = sequence
.WhereLead(1, -20, (cur, lead) => cur + lead >= 10);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [5, 6, 7, 8, 9]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|