Enumerable.GroupBy Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Groups the elements of a sequence.
Overloads
| Name | Description |
|---|---|
|
GroupBy |
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The elements of each group are projected by using a specified function. |
|
GroupBy |
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function. |
|
GroupBy |
Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function. |
|
GroupBy |
Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer and each group's elements are projected by using a specified function. |
|
GroupBy |
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. |
|
GroupBy |
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The keys are compared by using a specified comparer. |
|
GroupBy |
Groups the elements of a sequence according to a specified key selector function. |
|
GroupBy |
Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer. |
GroupBy(IEnumerable, Func, Func, Func,TResult>)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The elements of each group are projected by using a specified function.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector, Func ^ elementSelector, Func ^, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func elementSelector, Func,TResult> resultSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * Func<'Key, seq<'Element>, 'Result> -> seq<'Result>
Public Function GroupBy(Of TSource, TKey, TElement, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement), resultSelector As Func(Of TKey, IEnumerable(Of TElement), TResult)) As IEnumerable(Of TResult)
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
- TElement
The type of the elements in each IGrouping
- TResult
The type of the result value returned by resultSelector.
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
- resultSelector
-
Func
IEnumerable ,TResult>
A function to create a result value from each group.
Returns
A collection of elements of type TResult where each element represents a projection over a group and its key.
Exceptions
source or keySelector or elementSelector or resultSelector is null.
Examples
The following code example demonstrates how to use GroupByTResult.
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
public static void GroupByEx4()
{
// Create a list of pets.
List petsList =
new List{ new Pet { Name="Barley", Age=8.3 },
new Pet { Name="Boots", Age=4.9 },
new Pet { Name="Whiskers", Age=1.5 },
new Pet { Name="Daisy", Age=4.3 } };
// Group Pet.Age values by the Math.Floor of the age.
// Then project an anonymous type from each group
// that consists of the key, the count of the group's
// elements, and the minimum and maximum age in the group.
var query = petsList.GroupBy(
pet => Math.Floor(pet.Age),
pet => pet.Age,
(baseAge, ages) => new
{
Key = baseAge,
Count = ages.Count(),
Min = ages.Min(),
Max = ages.Max()
});
// Iterate over each anonymous type.
foreach (var result in query)
{
Console.WriteLine("\nAge group: " + result.Key);
Console.WriteLine("Number of pets in this age group: " + result.Count);
Console.WriteLine("Minimum age: " + result.Min);
Console.WriteLine("Maximum age: " + result.Max);
}
/* This code produces the following output:
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Structure Pet
Public Name As String
Public Age As Double
End Structure
Public Sub GroupByEx4()
' Create a list of pets.
Dim petsList As New List(Of Pet)(New Pet() _
{New Pet With {.Name = "Barley", .Age = 8.3},
New Pet With {.Name = "Boots", .Age = 4.9},
New Pet With {.Name = "Whiskers", .Age = 1.5},
New Pet With {.Name = "Daisy", .Age = 4.3}})
' Group Pet.Age values by the Math.Floor of the age.
' Then project an anonymous type from each group
' that consists of the key, the count of the group's
' elements, and the minimum and maximum age in the group.
Dim query = petsList.GroupBy(
Function(pet) Math.Floor(pet.Age),
Function(pet) pet.Age,
Function(baseAge, ages) New With
{.Key = baseAge,
.Count = ages.Count(),
.Min = ages.Min(),
.Max = ages.Max()}
)
Dim output As New System.Text.StringBuilder
' Iterate over each anonymous type.
For Each result In query
output.AppendLine(vbCrLf & "Age group: " & result.Key)
output.AppendLine("Number of pets in this age group: " & result.Count)
output.AppendLine("Minimum age: " & result.Min)
output.AppendLine("Maximum age: " & result.Max)
Next
' Display the output.
Console.WriteLine(output.ToString)
End Sub
' This code produces the following output:
' Age group: 8
' Number of pets in this age group: 1
' Minimum age: 8.3
' Maximum age: 8.3
'
' Age group: 4
' Number of pets in this age group: 2
' Minimum age: 4.3
' Maximum age: 4.9
'
' Age group: 1
' Number of pets in this age group: 1
' Minimum age: 1.5
' Maximum age: 1.5
Remarks
In query expression syntax, a group by (C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy.
See also
Applies to
GroupBy(IEnumerable, Func, Func, Func,
TResult>, IEqualityComparer)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector, Func ^ elementSelector, Func ^, TResult> ^ resultSelector, System::Collections::Generic::IEqualityComparer ^ comparer);
public static System.Collections.Generic.IEnumerable GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func elementSelector, Func,TResult> resultSelector, System.Collections.Generic.IEqualityComparer comparer);
public static System.Collections.Generic.IEnumerable GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func elementSelector, Func,TResult> resultSelector, System.Collections.Generic.IEqualityComparer? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * Func<'Key, seq<'Element>, 'Result> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<'Result>
Public Function GroupBy(Of TSource, TKey, TElement, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement), resultSelector As Func(Of TKey, IEnumerable(Of TElement), TResult), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of TResult)
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
- TElement
The type of the elements in each IGrouping
- TResult
The type of the result value returned by resultSelector.
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
- resultSelector
-
Func
IEnumerable ,TResult>
A function to create a result value from each group.
- comparer
-
IEqualityComparer
An IEqualityComparer
Returns
A collection of elements of type TResult where each element represents a projection over a group and its key.
Exceptions
source or keySelector or elementSelector or resultSelector is null.
See also
Applies to
GroupBy(IEnumerable, Func, Func)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^> ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector, Func ^ elementSelector);
public static System.Collections.Generic.IEnumerable> GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func elementSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> -> seq>
Public Function GroupBy(Of TSource, TKey, TElement) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement)) As IEnumerable(Of IGrouping(Of TKey, TElement))
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
- TElement
The type of the elements in the IGrouping
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
Returns
An IEnumerable in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each IGroupingTElement and a key.
Exceptions
source or keySelector or elementSelector is null.
Examples
The following code example demonstrates how to use GroupBy
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
// Uses method-based query syntax.
public static void GroupByEx1()
{
// Create a list of pets.
List pets =
new List{ new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 },
new Pet { Name="Daisy", Age=4 } };
// Group the pets using Age as the key value
// and selecting only the pet's Name for each value.
IEnumerable> query =
pets.GroupBy(pet => pet.Age, pet => pet.Name);
// Iterate over each IGrouping in the collection.
foreach (IGrouping petGroup in query)
{
// Print the key value of the IGrouping.
Console.WriteLine(petGroup.Key);
// Iterate over each value in the
// IGrouping and print the value.
foreach (string name in petGroup)
Console.WriteLine(" {0}", name);
}
}
/*
This code produces the following output:
8
Barley
4
Boots
Daisy
1
Whiskers
*/
Structure Pet
Public Name As String
Public Age As Integer
End Structure
Sub GroupByEx1()
'Create a list of Pet objects.
Dim pets As New List(Of Pet)(New Pet() _
{New Pet With {.Name = "Barley", .Age = 8},
New Pet With {.Name = "Boots", .Age = 4},
New Pet With {.Name = "Whiskers", .Age = 1},
New Pet With {.Name = "Daisy", .Age = 4}})
' Group the pets using Age as the key
' and selecting only the pet's Name for each value.
Dim query As IEnumerable(Of IGrouping(Of Integer, String)) =
pets.GroupBy(Function(pet) pet.Age,
Function(pet) pet.Name)
Dim output As New System.Text.StringBuilder
' Iterate over each IGrouping in the collection.
For Each petGroup As IGrouping(Of Integer, String) In query
' Print the key value of the IGrouping.
output.AppendLine(petGroup.Key)
' Iterate over each value in the IGrouping and print the value.
For Each name As String In petGroup
output.AppendLine(" " & name)
Next
Next
' Display the output.
Console.WriteLine(output.ToString)
End Sub
' This code produces the following output:
'
' 8
' Barley
' 4
' Boots
' Daisy
' 1
' Whiskers
In query expression syntax, a group by (C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. The translation of the query expression in the following example is equivalent to the query in the example above.
IEnumerable> query =
from pet in pets
group pet.Name by pet.Age;
Dim query =
From pet In pets
Group pet.Name By Age = pet.Age Into ageGroup = Group
Note
In a C# or Visual Basic query expression, the element and key selection expressions occur in the reverse order from their argument positions in a call to the GroupBy
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.
The GroupBy
The IGroupingsource that produced the first key of each IGroupingsource.
The default equality comparer Default is used to compare keys.
See also
Applies to
GroupBy(IEnumerable, Func, Func, IEqualityComparer)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer and each group's elements are projected by using a specified function.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^> ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector, Func ^ elementSelector, System::Collections::Generic::IEqualityComparer ^ comparer);
public static System.Collections.Generic.IEnumerable> GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func elementSelector, System.Collections.Generic.IEqualityComparer comparer);
public static System.Collections.Generic.IEnumerable> GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func elementSelector, System.Collections.Generic.IEqualityComparer? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * System.Collections.Generic.IEqualityComparer<'Key> -> seq>
Public Function GroupBy(Of TSource, TKey, TElement) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of IGrouping(Of TKey, TElement))
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
- TElement
The type of the elements in the IGrouping
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
- comparer
-
IEqualityComparer
An IEqualityComparer
Returns
An IEnumerable in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each IGroupingTElement and a key.
Exceptions
source or keySelector or elementSelector is null.
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.
Note
For examples of GroupBy, see the following articles:
The GroupBy
The IGroupingsource that produced the first key of each IGroupingsource.
If comparer is null, the default equality comparer Default is used to compare keys.
If two keys are considered equal according to comparer, the first key is chosen as the key for that grouping.
In query expression syntax, a group by (C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. For more information and usage examples, see group clause and Group By Clause.
See also
Applies to
GroupBy(IEnumerable, Func, Func,TResult>)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector, Func ^, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func,TResult> resultSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Key, seq<'Source>, 'Result> -> seq<'Result>
Public Function GroupBy(Of TSource, TKey, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), resultSelector As Func(Of TKey, IEnumerable(Of TSource), TResult)) As IEnumerable(Of TResult)
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
- TResult
The type of the result value returned by resultSelector.
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
- resultSelector
-
Func
IEnumerable ,TResult>
A function to create a result value from each group.
Returns
A collection of elements of type TResult where each element represents a projection over a group and its key.
Exceptions
source or keySelector or resultSelector is null.
Examples
The following code example demonstrates how to use GroupByTResult.
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
public static void GroupByEx3()
{
// Create a list of pets.
List petsList =
new List{ new Pet { Name="Barley", Age=8.3 },
new Pet { Name="Boots", Age=4.9 },
new Pet { Name="Whiskers", Age=1.5 },
new Pet { Name="Daisy", Age=4.3 } };
// Group Pet objects by the Math.Floor of their age.
// Then project an anonymous type from each group
// that consists of the key, the count of the group's
// elements, and the minimum and maximum age in the group.
var query = petsList.GroupBy(
pet => Math.Floor(pet.Age),
(age, pets) => new
{
Key = age,
Count = pets.Count(),
Min = pets.Min(pet => pet.Age),
Max = pets.Max(pet => pet.Age)
});
// Iterate over each anonymous type.
foreach (var result in query)
{
Console.WriteLine("\nAge group: " + result.Key);
Console.WriteLine("Number of pets in this age group: " + result.Count);
Console.WriteLine("Minimum age: " + result.Min);
Console.WriteLine("Maximum age: " + result.Max);
}
/* This code produces the following output:
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Structure Pet
Public Name As String
Public Age As Double
End Structure
Public Sub GroupByEx3()
' Create a list of pets.
Dim petsList As New List(Of Pet)(New Pet() _
{New Pet With {.Name = "Barley", .Age = 8.3},
New Pet With {.Name = "Boots", .Age = 4.9},
New Pet With {.Name = "Whiskers", .Age = 1.5},
New Pet With {.Name = "Daisy", .Age = 4.3}})
' Group Pet objects by the Math.Floor of their age.
' Then project an anonymous type from each group
' that consists of the key, the count of the group's
' elements, and the minimum and maximum age in the group.
Dim query = petsList.GroupBy(
Function(pet) Math.Floor(pet.Age),
Function(age, pets) New With
{.Key = age,
.Count = pets.Count(),
.Min = pets.Min(Function(pet) pet.Age),
.Max = pets.Max(Function(Pet) Pet.Age)}
)
Dim output As New System.Text.StringBuilder
' Iterate over each anonymous type.
For Each result In query
output.AppendLine(vbCrLf & "Age group: " & result.Key)
output.AppendLine("Number of pets in this age group: " & result.Count)
output.AppendLine("Minimum age: " & result.Min)
output.AppendLine("Maximum age: " & result.Max)
Next
' Display the output.
Console.WriteLine(output.ToString)
End Sub
' This code produces the following output:
' Age group: 8
' Number of pets in this age group: 1
' Minimum age: 8.3
' Maximum age: 8.3
'
' Age group: 4
' Number of pets in this age group: 2
' Minimum age: 4.3
' Maximum age: 4.9
'
' Age group: 1
' Number of pets in this age group: 1
' Minimum age: 1.5
' Maximum age: 1.5
Remarks
In query expression syntax, a group by (C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy.
See also
Applies to
GroupBy(IEnumerable, Func, Func,TResult>, IEqualityComparer)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The keys are compared by using a specified comparer.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector, Func ^, TResult> ^ resultSelector, System::Collections::Generic::IEqualityComparer ^ comparer);
public static System.Collections.Generic.IEnumerable GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func,TResult> resultSelector, System.Collections.Generic.IEqualityComparer comparer);
public static System.Collections.Generic.IEnumerable GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, Func,TResult> resultSelector, System.Collections.Generic.IEqualityComparer? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Key, seq<'Source>, 'Result> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<'Result>
Public Function GroupBy(Of TSource, TKey, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), resultSelector As Func(Of TKey, IEnumerable(Of TSource), TResult), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of TResult)
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
- TResult
The type of the result value returned by resultSelector.
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
- resultSelector
-
Func
IEnumerable ,TResult>
A function to create a result value from each group.
- comparer
-
IEqualityComparer
An IEqualityComparer
Returns
A collection of elements of type TResult where each element represents a projection over a group and its key.
Exceptions
source or keySelector or resultSelector is null.
See also
Applies to
GroupBy(IEnumerable, Func)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a specified key selector function.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^> ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector);
public static System.Collections.Generic.IEnumerable> GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> -> seq>
Public Function GroupBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As IEnumerable(Of IGrouping(Of TKey, TSource))
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
Returns
An IEnumerable in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each IGrouping
Exceptions
source or keySelector is null.
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.
Note
For examples of GroupBy, see the following articles:
The GroupBy
The IGroupingsource that produced the first key of each IGroupingsource.
The default equality comparer Default is used to compare keys.
In query expression syntax, a group by (C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. For more information and usage examples, see group clause and Group By Clause.
See also
Applies to
GroupBy(IEnumerable, Func, IEqualityComparer)
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
- Source:
- Grouping.cs
Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.
public:
generic
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable ^> ^ GroupBy(System::Collections::Generic::IEnumerable ^ source, Func ^ keySelector, System::Collections::Generic::IEqualityComparer ^ comparer);
public static System.Collections.Generic.IEnumerable> GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, System.Collections.Generic.IEqualityComparer comparer);
public static System.Collections.Generic.IEnumerable> GroupBy(this System.Collections.Generic.IEnumerable source, Func keySelector, System.Collections.Generic.IEqualityComparer? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IEqualityComparer<'Key> -> seq>
Public Function GroupBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of IGrouping(Of TKey, TSource))
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of the key returned by keySelector.
Parameters
- source
-
IEnumerable
An IEnumerable
- keySelector
-
Func
A function to extract the key for each element.
- comparer
-
IEqualityComparer
An IEqualityComparer
Returns
An IEnumerable in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each IGrouping
Exceptions
source or keySelector is null.
Remarks
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.
Note
For examples of GroupBy, see the following articles:
The GroupBy
The IGroupingsource that produced the first key of each IGroupingsource.
If comparer is null, the default equality comparer Default is used to compare keys.
If two keys are considered equal according to comparer, the first key is chosen as the key for that grouping.
In query expression syntax, a group by (C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. For more information and usage examples, see group clause and Group By Clause.