Method WhereLag
| Edit this page View SourceWhereLag<TSource>(IEnumerable<TSource>, int, Func<TSource, TSource?, bool>)
Filters a sequence of values based on a predicate evaluated on the current value and a lagging value.
Declaration
public static IEnumerable<TSource> WhereLag<TSource>(this IEnumerable<TSource> source, int offset, Func<TSource, TSource?, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | An IEnumerable<T> to filter. |
int | offset | The offset (expressed as a positive number) by which to lag each element of the sequence. |
Func<TSource, TSource, bool> | predicate | A function which accepts the current and lagged 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 Lag value.
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to filter a sequence based on a lagging value in the sequence using WhereLag
.
var sequence = Enumerable.Range(1, 10);
// Filter a sequence based on a leading value
var result = sequence
.WhereLag(1, (cur, lag) => cur + lag < 10);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, 4, 5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
WhereLag<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 lagging value.
Declaration
public static IEnumerable<TSource> WhereLag<TSource>(this IEnumerable<TSource> source, int offset, TSource defaultLagValue, Func<TSource, TSource, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | An IEnumerable<T> to filter. |
int | offset | The offset (expressed as a positive number) by which to lag each element of the sequence. |
TSource | defaultLagValue | A default value supplied for the lagged element when none is available |
Func<TSource, TSource, bool> | predicate | A function which accepts the current and lagged 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 lagging value in the sequence using WhereLag
.
var sequence = Enumerable.Range(1, 10);
// Filter a sequence based on a leading value
var result = sequence
.WhereLag(1, 20, (cur, lag) => cur + lag < 10);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [2, 3, 4, 5]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|