Enumerable.GroupBy Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Grupuje elementy sekwencji.
Przeciążenia
| Nazwa | Opis |
|---|---|
| GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>) |
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza. Elementy każdej grupy są projektowane przy użyciu określonej funkcji. |
| GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>, TResult>, IEqualityComparer<TKey>) |
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza. Wartości klucza są porównywane przy użyciu określonego porównania, a elementy każdej grupy są przewidywane przy użyciu określonej funkcji. |
| GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) |
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i projektuje elementy dla każdej grupy przy użyciu określonej funkcji. |
| GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>) |
Grupuje elementy sekwencji zgodnie z funkcją selektora klucza. Klucze są porównywane przy użyciu modułu porównującego, a elementy każdej grupy są projektowane przy użyciu określonej funkcji. |
| GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>) |
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza. |
| GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>) |
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza. Klucze są porównywane przy użyciu określonego porównania. |
| GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) |
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy. |
| GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) |
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i porównuje klucze przy użyciu określonego porównania. |
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza. Elementy każdej grupy są projektowane przy użyciu określonej funkcji.
public:
generic <typename TSource, typename TKey, typename TElement, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector, Func<TKey, System::Collections::Generic::IEnumerable<TElement> ^, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TElement,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * Func<'Key, seq<'Element>, 'Result> -> seq<'Result>
<Extension()>
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)
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
- TElement
Typ elementów w każdym IGrouping<TKey,TElement>obiekcie .
- TResult
Typ wartości wyniku zwracanej przez resultSelector.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
- elementSelector
- Func<TSource,TElement>
Funkcja mapowania każdego elementu źródłowego na element w elemecie IGrouping<TKey,TElement>.
- resultSelector
- Func<TKey,IEnumerable<TElement>,TResult>
Funkcja umożliwiająca utworzenie wartości wyniku z każdej grupy.
Zwraca
Kolekcja elementów typu TResult , w których każdy element reprezentuje projekcję w grupie i jej kluczu.
Wyjątki
source lub keySelectorelementSelectorresultSelector lub ma wartość null.
Przykłady
W poniższym przykładzie kodu pokazano, jak grupować przewidywane elementy sekwencji, a następnie projektować GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>) sekwencję wyników typu TResult.
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
public static void GroupByEx4()
{
// Create a list of pets.
List<Pet> petsList =
new List<Pet>{ 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
Uwagi
W składni wyrażenia zapytania klauzula group by (C#) lub Group By Into (Visual Basic) przekłada się na wywołanie GroupBy.
Zobacz też
Dotyczy
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>, TResult>, IEqualityComparer<TKey>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza. Wartości klucza są porównywane przy użyciu określonego porównania, a elementy każdej grupy są przewidywane przy użyciu określonej funkcji.
public:
generic <typename TSource, typename TKey, typename TElement, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector, Func<TKey, System::Collections::Generic::IEnumerable<TElement> ^, TResult> ^ resultSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TElement,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TElement,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * Func<'Key, seq<'Element>, 'Result> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<'Result>
<Extension()>
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)
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
- TElement
Typ elementów w każdym IGrouping<TKey,TElement>obiekcie .
- TResult
Typ wartości wyniku zwracanej przez resultSelector.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
- elementSelector
- Func<TSource,TElement>
Funkcja mapowania każdego elementu źródłowego na element w elemecie IGrouping<TKey,TElement>.
- resultSelector
- Func<TKey,IEnumerable<TElement>,TResult>
Funkcja umożliwiająca utworzenie wartości wyniku z każdej grupy.
- comparer
- IEqualityComparer<TKey>
Element IEqualityComparer<T> do porównywania kluczy z.
Zwraca
Kolekcja elementów typu TResult , w których każdy element reprezentuje projekcję w grupie i jej kluczu.
Wyjątki
source lub keySelectorelementSelectorresultSelector lub ma wartość null.
Zobacz też
Dotyczy
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i projektuje elementy dla każdej grupy przy użyciu określonej funkcji.
public:
generic <typename TSource, typename TKey, typename TElement>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TElement> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> -> seq<System.Linq.IGrouping<'Key, 'Element>>
<Extension()>
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))
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
- TElement
Typ elementów w obiekcie IGrouping<TKey,TElement>.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
- elementSelector
- Func<TSource,TElement>
Funkcja mapowania każdego elementu źródłowego na element w elemecie IGrouping<TKey,TElement>.
Zwraca
IEnumerable<IGrouping<TKey, TElement>> w języku C# lub IEnumerable(Of IGrouping(Of TKey, TElement)) w Visual Basic gdzie każdy obiekt IGrouping<TKey,TElement> zawiera kolekcję obiektów typu TElement i klucz.
Wyjątki
source lub keySelectorelementSelector ma wartość null.
Przykłady
Poniższy przykład kodu przedstawia sposób grupowania GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) elementów sekwencji.
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<Pet> pets =
new List<Pet>{ 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<IGrouping<int, string>> query =
pets.GroupBy(pet => pet.Age, pet => pet.Name);
// Iterate over each IGrouping in the collection.
foreach (IGrouping<int, string> 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
W składni wyrażenia zapytania klauzula group by (C#) lub Group By Into (Visual Basic) przekłada się na wywołanie GroupBy. Tłumaczenie wyrażenia zapytania w poniższym przykładzie jest równoważne zapytaniu w powyższym przykładzie.
IEnumerable<IGrouping<int, string>> 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
W wyrażeniu zapytania w języku C# lub Visual Basic wyrażenia wyboru elementu i klucza występują w odwrotnej kolejności od pozycji argumentów w wywołaniu metody GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>).
Uwagi
Ta metoda jest implementowana przy użyciu odroczonego wykonania. Natychmiastowa wartość zwracana to obiekt, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach w języku C# lub For Each w Visual Basic.
Metoda GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) zwraca kolekcję IGrouping<TKey,TElement> obiektów— jedną dla każdego napotkanego klucza. Element IGrouping<TKey,TElement> to element IEnumerable<T> , który ma również klucz skojarzony z jego elementami.
Obiekty IGrouping<TKey,TElement> są zwracane w kolejności na podstawie kolejności elementów, w source których wygenerowano pierwszy klucz każdego IGrouping<TKey,TElement>z nich. Elementy w grupowaniu są zwracane w kolejności, w jaką elementy, które je wygenerowały, są wyświetlane w elemecie source.
Domyślny moduł porównywania Default równości służy do porównywania kluczy.
Zobacz też
Dotyczy
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z funkcją selektora klucza. Klucze są porównywane przy użyciu modułu porównującego, a elementy każdej grupy są projektowane przy użyciu określonej funkcji.
public:
generic <typename TSource, typename TKey, typename TElement>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TElement> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<System.Linq.IGrouping<'Key, 'Element>>
<Extension()>
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))
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
- TElement
Typ elementów w obiekcie IGrouping<TKey,TElement>.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
- elementSelector
- Func<TSource,TElement>
Funkcja mapowania każdego elementu źródłowego na element w elemecie IGrouping<TKey,TElement>.
- comparer
- IEqualityComparer<TKey>
Element IEqualityComparer<T> do porównywania kluczy.
Zwraca
IEnumerable<IGrouping<TKey, TElement>> w języku C# lub IEnumerable(Of IGrouping(Of TKey, TElement)) w Visual Basic gdzie każdy obiekt IGrouping<TKey,TElement> zawiera kolekcję obiektów typu TElement i klucz.
Wyjątki
source lub keySelectorelementSelector ma wartość null.
Uwagi
Ta metoda jest implementowana przy użyciu odroczonego wykonania. Natychmiastowa wartość zwracana to obiekt, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach w języku C# lub For Each w Visual Basic.
Note
Przykłady usługi GroupBymożna znaleźć w następujących artykułach:
- GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
Metoda GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>) zwraca kolekcję IGrouping<TKey,TElement> obiektów— jedną dla każdego napotkanego klucza. Element IGrouping<TKey,TElement> to element IEnumerable<T> , który ma również klucz skojarzony z jego elementami.
Obiekty IGrouping<TKey,TElement> są zwracane w kolejności na podstawie kolejności elementów, w source których wygenerowano pierwszy klucz każdego IGrouping<TKey,TElement>z nich. Elementy w grupowaniu są zwracane w kolejności, w jaką elementy, które je wygenerowały, są wyświetlane w elemecie source.
Jeśli comparer parametr ma nullwartość , domyślnym modułem porównywania Default równości jest używany do porównywania kluczy.
Jeśli dwa klucze są traktowane jako równe zgodnie z comparer, pierwszy klucz jest wybierany jako klucz dla tego grupowania.
W składni wyrażenia zapytania klauzula group by (C#) lub Group By Into (Visual Basic) przekłada się na wywołanie GroupBy. Aby uzyskać więcej informacji i przykładów użycia, zobacz klauzulę group i klauzulę Group By.
Zobacz też
Dotyczy
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza.
public:
generic <typename TSource, typename TKey, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TKey, System::Collections::Generic::IEnumerable<TSource> ^, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Key, seq<'Source>, 'Result> -> seq<'Result>
<Extension()>
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)
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
- TResult
Typ wartości wyniku zwracanej przez resultSelector.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
- resultSelector
- Func<TKey,IEnumerable<TSource>,TResult>
Funkcja umożliwiająca utworzenie wartości wyniku z każdej grupy.
Zwraca
Kolekcja elementów typu TResult , w których każdy element reprezentuje projekcję w grupie i jej kluczu.
Wyjątki
source lub keySelectorresultSelector ma wartość null.
Przykłady
W poniższym przykładzie kodu pokazano, jak grupować GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>) elementy sekwencji i projektować sekwencję wyników typu TResult.
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
public static void GroupByEx3()
{
// Create a list of pets.
List<Pet> petsList =
new List<Pet>{ 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
Uwagi
W składni wyrażenia zapytania klauzula group by (C#) lub Group By Into (Visual Basic) przekłada się na wywołanie GroupBy.
Zobacz też
Dotyczy
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i tworzy wartość wynikową z każdej grupy i jej klucza. Klucze są porównywane przy użyciu określonego porównania.
public:
generic <typename TSource, typename TKey, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TKey, System::Collections::Generic::IEnumerable<TSource> ^, TResult> ^ resultSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Key, seq<'Source>, 'Result> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<'Result>
<Extension()>
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)
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
- TResult
Typ wartości wyniku zwracanej przez resultSelector.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
- resultSelector
- Func<TKey,IEnumerable<TSource>,TResult>
Funkcja umożliwiająca utworzenie wartości wyniku z każdej grupy.
- comparer
- IEqualityComparer<TKey>
Element IEqualityComparer<T> do porównywania kluczy z.
Zwraca
Kolekcja elementów typu TResult , w których każdy element reprezentuje projekcję w grupie i jej kluczu.
Wyjątki
source lub keySelectorresultSelector ma wartość null.
Zobacz też
Dotyczy
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy.
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TSource> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> -> seq<System.Linq.IGrouping<'Key, 'Source>>
<Extension()>
Public Function GroupBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As IEnumerable(Of IGrouping(Of TKey, TSource))
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
Zwraca
Obiekt IEnumerable<IGrouping<TKey, TSource>> w języku C# lub IEnumerable(Of IGrouping(Of TKey, TSource)) Visual Basic, w którym każdy IGrouping<TKey,TElement> obiekt zawiera sekwencję obiektów i klucz.
Wyjątki
source lub keySelector ma wartość null.
Uwagi
Ta metoda jest implementowana przy użyciu odroczonego wykonania. Natychmiastowa wartość zwracana to obiekt, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach w języku C# lub For Each w Visual Basic.
Note
Przykłady usługi GroupBymożna znaleźć w następujących artykułach:
- GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
Metoda GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) zwraca kolekcję IGrouping<TKey,TElement> obiektów— jedną dla każdego napotkanego klucza. Element IGrouping<TKey,TElement> to element IEnumerable<T> , który ma również klucz skojarzony z jego elementami.
Obiekty IGrouping<TKey,TElement> są zwracane w kolejności na podstawie kolejności elementów, w source których wygenerowano pierwszy klucz każdego IGrouping<TKey,TElement>z nich. Elementy w grupowaniu są zwracane w kolejności, w której są wyświetlane w sourcepliku .
Domyślny moduł porównywania Default równości służy do porównywania kluczy.
W składni wyrażenia zapytania klauzula group by (C#) lub Group By Into (Visual Basic) przekłada się na wywołanie GroupBy. Aby uzyskać więcej informacji i przykładów użycia, zobacz klauzulę group i klauzulę Group By.
Zobacz też
Dotyczy
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
- Źródło:
- Grouping.cs
Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i porównuje klucze przy użyciu określonego porównania.
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TSource> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<System.Linq.IGrouping<'Key, 'Source>>
<Extension()>
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))
Parametry typu
- TSource
Typ elementów elementu source.
- TKey
Typ klucza zwróconego przez keySelector.
Parametry
- source
- IEnumerable<TSource>
Element IEnumerable<T> , którego elementy do grupowania.
- keySelector
- Func<TSource,TKey>
Funkcja wyodrębniania klucza dla każdego elementu.
- comparer
- IEqualityComparer<TKey>
Element IEqualityComparer<T> do porównywania kluczy.
Zwraca
IEnumerable<IGrouping<TKey, TSource>> w języku C# lub IEnumerable(Of IGrouping(Of TKey, TSource)) w Visual Basic, gdzie każdy obiekt IGrouping<TKey,TElement> zawiera kolekcję obiektów i klucz.
Wyjątki
source lub keySelector ma wartość null.
Uwagi
Ta metoda jest implementowana przy użyciu odroczonego wykonania. Natychmiastowa wartość zwracana to obiekt, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach w języku C# lub For Each w Visual Basic.
Note
Przykłady usługi GroupBymożna znaleźć w następujących artykułach:
- GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
Metoda GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) zwraca kolekcję IGrouping<TKey,TElement> obiektów— jedną dla każdego napotkanego klucza. Element IGrouping<TKey,TElement> to element IEnumerable<T> , który ma również klucz skojarzony z jego elementami.
Obiekty IGrouping<TKey,TElement> są zwracane w kolejności na podstawie kolejności elementów, w source których wygenerowano pierwszy klucz każdego IGrouping<TKey,TElement>z nich. Elementy w grupowaniu są zwracane w kolejności, w której są wyświetlane w sourcepliku .
Jeśli comparer parametr ma nullwartość , domyślnym modułem porównywania Default równości jest używany do porównywania kluczy.
Jeśli dwa klucze są traktowane jako równe zgodnie z comparer, pierwszy klucz jest wybierany jako klucz dla tego grupowania.
W składni wyrażenia zapytania klauzula group by (C#) lub Group By Into (Visual Basic) przekłada się na wywołanie GroupBy. Aby uzyskać więcej informacji i przykładów użycia, zobacz klauzulę group i klauzulę Group By.