Method Lead
| Edit this page View SourceLead<TSource>(IEnumerable<TSource>, int)
Produces a projection of a sequence by evaluating pairs of elements separated by a positive offset.
Declaration
public static IEnumerable<(TSource current, TSource? lead)> Lead<TSource>(this IEnumerable<TSource> source, int offset)
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 |
Returns
Type | Description |
---|---|
IEnumerable<(TSource current, TSource lag)> | A sequence of tuples with the current and lead elements |
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(
is used as the lead value.TSource
?)
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to get elements along with a future value using Lead
.
var sequence = new[] { "foo", "bar", "baz" };
// get leading elements
var result = sequence.Lead(1);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(foo, bar), (bar, baz), (baz, )]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Lead<TSource, TResult>(IEnumerable<TSource>, int, Func<TSource, TSource?, TResult>)
Produces a projection of a sequence by evaluating pairs of elements separated by a positive offset.
Declaration
public static IEnumerable<TResult> Lead<TSource, TResult>(this IEnumerable<TSource> source, int offset, Func<TSource, TSource?, TResult> resultSelector)
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, TResult> | resultSelector | A projection function which accepts the current and subsequent (lead) element (in that order) and produces a result |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence produced by projecting each element of the sequence with its lead pairing |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements in the source sequence |
TResult | The type of the elements in the result sequence |
Remarks
For elements of the sequence that are less than offset
items from the end, default(
is used as the lead value.TSource
?)
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to get elements along with a future value using Lead
.
var sequence = new[] { "foo", "bar", "baz" };
// get leading elements
var result = sequence.Lead(1, (cur, lead) => new { cur, lead, });
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [{ cur = foo, lead = bar }, { cur = bar, lead = baz }, { cur = baz, lead = }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Lead<TSource, TResult>(IEnumerable<TSource>, int, TSource, Func<TSource, TSource, TResult>)
Produces a projection of a sequence by evaluating pairs of elements separated by a positive offset.
Declaration
public static IEnumerable<TResult> Lead<TSource, TResult>(this IEnumerable<TSource> source, int offset, TSource defaultLeadValue, Func<TSource, TSource, TResult> resultSelector)
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, TResult> | resultSelector | A projection function which accepts the current and subsequent (lead) element (in that order) and produces a result |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence produced by projecting each element of the sequence with its lead pairing |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements in the source sequence |
TResult | The type of the elements in the result sequence |
Remarks
For elements of the sequence that are less than offset
items from the end, defaultLeadValue
is used as the lead value.
This operator evaluates in a deferred and streaming manner.
Examples
The following code example demonstrates how to get elements along with a future value using Lead
.
var sequence = new[] { "foo", "bar", "baz" };
// get leading elements
var result = sequence.Lead(1, "LEAD", (cur, lag) => new { cur, lag, });
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [{ cur = foo, lag = bar }, { cur = bar, lag = baz }, { cur = baz, lag = LEAD }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|