Method IndexBy
| Edit this page View SourceIndexBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Applies a key-generating function to each element of a sequence and returns a sequence that contains the elements of the original sequence as well its key and index inside the group of its key.
Declaration
public static IEnumerable<(int index, TSource item)> IndexBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
Func<TSource, TKey> | keySelector | Function that projects the key given an element in the source sequence. |
Returns
Type | Description |
---|---|
IEnumerable<(int Index, TSource Item)> | A sequence of elements paired with their index within the key-group. The index is the key and the element is the value of the pair. |
Type Parameters
Name | Description |
---|---|
TSource | Type of the source sequence elements. |
TKey | Type of the projected key. |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to return a sub-sequence index with each element using IndexBy
.
var sequence = new[]
{
new { Key = 1, Value = 123, },
new { Key = 2, Value = 987, },
new { Key = 3, Value = 789, },
new { Key = 1, Value = 123, },
new { Key = 1, Value = 781, },
};
// Group adjacent items
var result = sequence
.IndexBy(
x => x.Key);
Console.WriteLine(
"[ " +
string.Join(
", ",
result) +
" ]");
// This code produces the following output:
// [ (0, { Key = 1, Value = 123 }), (0, { Key = 2, Value = 987 }), (0, { Key = 3, Value = 789 }), (1, { Key = 1, Value = 123 }), (2, { Key = 1, Value = 781 }) ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
IndexBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)
Applies a key-generating function to each element of a sequence and returns a sequence that contains the elements of the original sequence as well its key and index inside the group of its key. An additional parameter specifies a comparer to use for testing the equivalence of keys.
Declaration
public static IEnumerable<(int index, TSource item)> IndexBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence. |
Func<TSource, TKey> | keySelector | Function that projects the key given an element in the source sequence. |
IEqualityComparer<TKey> | comparer | The equality comparer to use to determine whether or not keys are equal. If null, the
default equality comparer for |
Returns
Type | Description |
---|---|
IEnumerable<(int Index, TSource Item)> | A sequence of elements paired with their index within the key-group. The index is the key and the element is the value of the pair. |
Type Parameters
Name | Description |
---|---|
TSource | Type of the source sequence elements. |
TKey | Type of the projected key. |
Remarks
This operator uses deferred execution and streams its results.
Examples
The following code example demonstrates how to return a sub-sequence index with each element using IndexBy
.
var sequence = new[]
{
new { Key = "jan", Value = 123, },
new { Key = "feb", Value = 987, },
new { Key = "mar", Value = 789, },
new { Key = "Jan", Value = 123, },
new { Key = "JAN", Value = 781, },
};
// Group adjacent items
var result = sequence
.IndexBy(
x => x.Key,
StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[ " +
string.Join(
", ",
result) +
" ]");
// This code produces the following output:
// [ (0, { Key = jan, Value = 123 }), (0, { Key = feb, Value = 987 }), (0, { Key = mar, Value = 789 }), (1, { Key = Jan, Value = 123 }), (2, { Key = JAN, Value = 781 }) ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|