Method GroupAdjacent
| Edit this pageGroupAdjacent<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Groups the adjacent elements of a sequence according to a specified key selector function.
Declaration
public static IEnumerable<IGrouping<TKey, TSource>> GroupAdjacent<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | A sequence whose elements to group. |
| Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IGrouping<TKey, TSource>> | A sequence of groupings where each grouping (IGrouping<TKey, TElement>) contains the key and the adjacent elements in the same order as found in the source sequence. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of |
| TKey | The type of the key returned by |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Examples
The following code example shows how to group adjacent items in a sequence using GroupAdjacent:
var sequence = new[]
{
(key: 1, value: 123),
(key: 1, value: 456),
(key: 1, value: 789),
(key: 2, value: 987),
(key: 2, value: 654),
(key: 2, value: 321),
(key: 3, value: 789),
(key: 3, value: 456),
(key: 3, value: 123),
(key: 1, value: 123),
(key: 1, value: 456),
(key: 1, value: 781),
};
// Group adjacent items
var result = sequence
.GroupAdjacent(
x => x.key);
Console.WriteLine(
"[ " +
string.Join(
", ",
result.Select(c => "[" + string.Join(", ", c) + "]")) +
" ]");
// This code produces the following output:
// [ [(1, 123), (1, 456), (1, 789)], [(2, 987), (2, 654), (2, 321)], [(3, 789), (3, 456), (3, 123)], [(1, 123), (1, 456), (1, 781)] ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
GroupAdjacent<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)
Groups the adjacent elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.
Declaration
public static IEnumerable<IGrouping<TKey, TSource>> GroupAdjacent<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | A sequence whose elements to group. |
| Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
| IEqualityComparer<TKey> | comparer | An IEqualityComparer<T> to compare keys. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IGrouping<TKey, TSource>> | A sequence of groupings where each grouping (IGrouping<TKey, TElement>) contains the key and the adjacent elements in the same order as found in the source sequence. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of |
| TKey | The type of the key returned by |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Examples
The following code example shows how to group adjacent items in a sequence using GroupAdjacent:
var sequence = new[]
{
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 789),
(key: "feb", value: 987),
(key: "Feb", value: 654),
(key: "FEB", value: 321),
(key: "mar", value: 789),
(key: "Mar", value: 456),
(key: "MAR", value: 123),
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 781),
};
// Group adjacent items
var result = sequence
.GroupAdjacent(
x => x.key,
StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[ " +
string.Join(
", ",
result.Select(c => "[" + string.Join(", ", c) + "]")) +
" ]");
// This code produces the following output:
// [ [(jan, 123), (Jan, 456), (JAN, 789)], [(feb, 987), (Feb, 654), (FEB, 321)], [(mar, 789), (Mar, 456), (MAR, 123)], [(jan, 123), (Jan, 456), (JAN, 781)] ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
GroupAdjacent<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>)
Groups the adjacent elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.
Declaration
public static IEnumerable<IGrouping<TKey, TElement>> GroupAdjacent<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | A sequence whose elements to group. |
| Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
| Func<TSource, TElement> | elementSelector | A function to map each source element to an element in the resulting grouping. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IGrouping<TKey, TElement>> | A sequence of groupings where each grouping (IGrouping<TKey, TElement>) contains the key and
the adjacent elements (of type |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of |
| TKey | The type of the key returned by |
| TElement | The type of the elements in the resulting groupings. |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Examples
The following code example shows how to group adjacent items in a sequence using GroupAdjacent:
var sequence = new[]
{
(key: 1, value: 123),
(key: 1, value: 456),
(key: 1, value: 789),
(key: 2, value: 987),
(key: 2, value: 654),
(key: 2, value: 321),
(key: 3, value: 789),
(key: 3, value: 456),
(key: 3, value: 123),
(key: 1, value: 123),
(key: 1, value: 456),
(key: 1, value: 781),
};
// Group adjacent items
var result = sequence
.GroupAdjacent(
x => x.key,
x => x.value);
Console.WriteLine(
"[ " +
string.Join(
", ",
result.Select(c => "[" + string.Join(", ", c) + "]")) +
" ]");
// This code produces the following output:
// [ [(1, 123), (1, 456), (1, 789)], [(2, 987), (2, 654), (2, 321)], [(3, 789), (3, 456), (3, 123)], [(1, 123), (1, 456), (1, 781)] ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
GroupAdjacent<TSource, TKey, TElement>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>?)
Groups the adjacent elements of a sequence according to a specified key selector function. The keys are compared by using a comparer and each group's elements are projected by using a specified function.
Declaration
public static IEnumerable<IGrouping<TKey, TElement>> GroupAdjacent<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | A sequence whose elements to group. |
| Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
| Func<TSource, TElement> | elementSelector | A function to map each source element to an element in the resulting grouping. |
| IEqualityComparer<TKey> | comparer | An IEqualityComparer<T> to compare keys. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IGrouping<TKey, TElement>> | A sequence of groupings where each grouping (IGrouping<TKey, TElement>) contains the key and
the adjacent elements (of type |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of |
| TKey | The type of the key returned by |
| TElement | The type of the elements in the resulting groupings. |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Examples
The following code example shows how to group adjacent items in a sequence using GroupAdjacent:
var sequence = new[]
{
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 789),
(key: "feb", value: 987),
(key: "Feb", value: 654),
(key: "FEB", value: 321),
(key: "mar", value: 789),
(key: "Mar", value: 456),
(key: "MAR", value: 123),
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 781),
};
// Group adjacent items
var result = sequence
.GroupAdjacent(
x => x.key,
x => x.value,
StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[ " +
string.Join(
", ",
result.Select(c => "[" + string.Join(", ", c) + "]")) +
" ]");
// This code produces the following output:
// [ [123, 456, 789], [987, 654, 321], [789, 456, 123], [123, 456, 781] ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
GroupAdjacent<TSource, TKey, TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey, IReadOnlyList<TSource>, TResult>)
Groups the adjacent elements of a sequence according to a specified key selector function. The keys are compared by using a comparer and each group's elements are projected by using a specified function.
Declaration
public static IEnumerable<TResult> GroupAdjacent<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IReadOnlyList<TSource>, TResult> resultSelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | A sequence whose elements to group. |
| Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
| Func<TKey, IReadOnlyList<TSource>, TResult> | resultSelector | A function to map each key and associated source elements to a result object. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TResult> | A collection of elements of type |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of |
| TKey | The type of the key returned by |
| TResult | The type of the elements in the resulting sequence. |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
GroupAdjacent<TSource, TKey, TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey, IReadOnlyList<TSource>, TResult>, IEqualityComparer<TKey>?)
Groups the adjacent elements of a sequence according to a specified key selector function. The keys are compared by using a comparer and each group's elements are projected by using a specified function.
Declaration
public static IEnumerable<TResult> GroupAdjacent<TSource, TKey, TResult>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IReadOnlyList<TSource>, TResult> resultSelector, IEqualityComparer<TKey>? comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | A sequence whose elements to group. |
| Func<TSource, TKey> | keySelector | A function to extract the key for each element. |
| Func<TKey, IReadOnlyList<TSource>, TResult> | resultSelector | A function to map each key and associated source elements to a result object. |
| IEqualityComparer<TKey> | comparer | An IEqualityComparer<T> to compare keys. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TResult> | A collection of elements of type |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements of |
| TKey | The type of the key returned by |
| TResult | The type of the elements in the resulting sequence. |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|