Queryable.GroupBy Metoda

Definicja

Grupuje elementy sekwencji.

Przeciążenia

GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>)

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza. Klucze są porównywane przy użyciu określonego modułu porównującego, a elementy każdej grupy są rzutowane przy użyciu określonej funkcji.

GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>)

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza. Elementy każdej grupy są rzutowane przy użyciu określonej funkcji.

GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<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>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>)

Grupuje elementy sekwencji i projektuje elementy dla każdej grupy przy użyciu określonej funkcji. Wartości klucza są porównywane przy użyciu określonego modułu porównującego.

GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>)

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza.

GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>)

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza. Klucze są porównywane przy użyciu określonego modułu porównującego.

GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)

Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy.

GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)

Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i porównuje klucze przy użyciu określonego modułu porównującego.

GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza. Klucze są porównywane przy użyciu określonego modułu porównującego, a elementy każdej grupy są rzutowane przy użyciu określonej funkcji.

public:
generic <typename TSource, typename TKey, typename TElement, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TResult> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Linq::Expressions::Expression<Func<TSource, TElement> ^> ^ elementSelector, System::Linq::Expressions::Expression<Func<TKey, System::Collections::Generic::IEnumerable<TElement> ^, TResult> ^> ^ resultSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector, System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector, System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Linq.Expressions.Expression<Func<'Source, 'Element>> * System.Linq.Expressions.Expression<Func<'Key, seq<'Element>, 'Result>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Linq.IQueryable<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement, TResult) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), elementSelector As Expression(Of Func(Of TSource, TElement)), resultSelector As Expression(Of Func(Of TKey, IEnumerable(Of TElement), TResult)), comparer As IEqualityComparer(Of TKey)) As IQueryable(Of TResult)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

TElement

Typ elementów w każdym IGrouping<TKey,TElement>obiekcie .

TResult

Typ wartości wyniku zwracanej przez resultSelector.

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

elementSelector
Expression<Func<TSource,TElement>>

Funkcja mapowania każdego elementu źródłowego na element w elemecie IGrouping<TKey,TElement>.

resultSelector
Expression<Func<TKey,IEnumerable<TElement>,TResult>>

Funkcja tworzenia wartości wyniku z każdej grupy.

comparer
IEqualityComparer<TKey>

Element IEqualityComparer<T> do porównywania kluczy.

Zwraca

IQueryable<TResult>

Element T:System.Linq.IQueryable`1 , który ma argument TResult typu i gdzie każdy element reprezentuje projekcję dla grupy i jej klucza.

Wyjątki

source lub keySelectorelementSelector lub resultSelectorcomparer lub jest null.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza, które są uzyskiwane przez wywołanie keySelector każdego elementu. Parametr comparer służy do porównywania wartości kluczy. Parametr elementSelector służy do projekcji elementów każdej grupy, a resultSelector parametr służy do uzyskiwania wartości wynikowej z każdej grupy i jej klucza.

Dotyczy

GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza. Elementy każdej grupy są rzutowane przy użyciu określonej funkcji.

public:
generic <typename TSource, typename TKey, typename TElement, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TResult> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Linq::Expressions::Expression<Func<TSource, TElement> ^> ^ elementSelector, System::Linq::Expressions::Expression<Func<TKey, System::Collections::Generic::IEnumerable<TElement> ^, TResult> ^> ^ resultSelector);
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector, System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult>> resultSelector);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Linq.Expressions.Expression<Func<'Source, 'Element>> * System.Linq.Expressions.Expression<Func<'Key, seq<'Element>, 'Result>> -> System.Linq.IQueryable<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement, TResult) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), elementSelector As Expression(Of Func(Of TSource, TElement)), resultSelector As Expression(Of Func(Of TKey, IEnumerable(Of TElement), TResult))) As IQueryable(Of TResult)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

TElement

Typ elementów w każdym IGrouping<TKey,TElement>obiekcie .

TResult

Typ wartości wyniku zwracanej przez resultSelector.

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

elementSelector
Expression<Func<TSource,TElement>>

Funkcja mapowania każdego elementu źródłowego na element w elemecie IGrouping<TKey,TElement>.

resultSelector
Expression<Func<TKey,IEnumerable<TElement>,TResult>>

Funkcja tworzenia wartości wyniku z każdej grupy.

Zwraca

IQueryable<TResult>

Element T:System.Linq.IQueryable`1 , który ma argument TResult typu i gdzie każdy element reprezentuje projekcję dla grupy i jej klucza.

Wyjątki

source lub keySelectorelementSelector lub resultSelector ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą metody GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>) grupować elementy sekwencji i projektować 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.AsQueryable().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

Shared 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 valuesby 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.AsQueryable().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 with this age: " & result.Count)
        output.AppendLine("Minimum age: " & result.Min)
        output.AppendLine("Maximum age: " & result.Max)
    Next

    ' Display the output.
    MsgBox(output.ToString())

    ' This code produces the following output:

    '  Age group: 8
    '  Number of pets with this age: 1
    '  Minimum age: 8.3
    '  Maximum age: 8.3

    '  Age group: 4
    '  Number of pets with this age: 2
    '  Minimum age: 4.3
    '  Maximum age: 4.9

    '  Age group: 1
    '  Number of pets with this age: 1
    '  Minimum age: 1.5
    '  Maximum age: 1.5
End Sub

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey, IEnumerable<TElement>,TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza, które są uzyskiwane przez wywołanie keySelector każdego elementu. Parametr elementSelector służy do projekcji elementów każdej grupy, a resultSelector parametr służy do uzyskiwania wartości wynikowej z każdej grupy i jej klucza.

Dotyczy

GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.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::Linq::IQueryable<System::Linq::IGrouping<TKey, TElement> ^> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Linq::Expressions::Expression<Func<TSource, TElement> ^> ^ elementSelector);
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Linq.Expressions.Expression<Func<'Source, 'Element>> -> System.Linq.IQueryable<System.Linq.IGrouping<'Key, 'Element>>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), elementSelector As Expression(Of Func(Of TSource, TElement))) As IQueryable(Of IGrouping(Of TKey, TElement))

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

TElement

Typ elementów w każdym IGrouping<TKey,TElement>obiekcie .

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

elementSelector
Expression<Func<TSource,TElement>>

Funkcja mapowania każdego elementu źródłowego na element w elemecie IGrouping<TKey,TElement>.

Zwraca

IQueryable<IGrouping<TKey,TElement>>

Element IQueryable<IGrouping<TKey, TElement>> w języku C# lub IQueryable(Of IGrouping(Of TKey, TElement)) Visual Basic, w którym każda z nich IGrouping<TKey,TElement> zawiera sekwencję obiektów typu TElement i klucza.

Wyjątki

source lub keySelectorelementSelector ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) grupować elementy sekwencji.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void GroupByEx2()
{
    // Create a list of Pet objects.
    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 Pet.Age as the key.
    // Use Pet.Name as the value for each entry.
    IEnumerable<IGrouping<int, string>> query =
        pets.AsQueryable().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

Shared Sub GroupByEx2()
    ' 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 Pet.Age as the key.
    ' Use Pet.Name as the value for each entry.
    Dim query As IEnumerable(Of IGrouping(Of Integer, String)) = _
        pets.AsQueryable().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(String.Format("  {0}", name))
        Next
    Next

    ' Display the output.
    MsgBox(output.ToString())
End Sub

' This code produces the following output:
'
' 8
'  Barley
' 4
'  Boots
'  Daisy
' 1
'  Whiskers

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza, która jest uzyskiwana przez wywołanie keySelector każdego elementu. elementSelector Wywołuje on każdy element w celu uzyskania elementu wyniku.

Dotyczy

GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Grupuje elementy sekwencji i projektuje elementy dla każdej grupy przy użyciu określonej funkcji. Wartości klucza są porównywane przy użyciu określonego modułu porównującego.

public:
generic <typename TSource, typename TKey, typename TElement>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<System::Linq::IGrouping<TKey, TElement> ^> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Linq::Expressions::Expression<Func<TSource, TElement> ^> ^ elementSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TSource,TElement>> elementSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Linq.Expressions.Expression<Func<'Source, 'Element>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Linq.IQueryable<System.Linq.IGrouping<'Key, 'Element>>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), elementSelector As Expression(Of Func(Of TSource, TElement)), comparer As IEqualityComparer(Of TKey)) As IQueryable(Of IGrouping(Of TKey, TElement))

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

TElement

Typ elementów w każdym IGrouping<TKey,TElement>obiekcie .

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

elementSelector
Expression<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

IQueryable<IGrouping<TKey,TElement>>

Element IQueryable<IGrouping<TKey, TElement>> w języku C# lub IQueryable(Of IGrouping(Of TKey, TElement)) Visual Basic, w którym każda z nich IGrouping<TKey,TElement> zawiera sekwencję obiektów typu TElement i klucza.

Wyjątki

source lub keySelectorelementSelector lub comparer ma wartość null.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza, która jest uzyskiwana przez wywołanie keySelector każdego elementu. Wartości klucza są porównywane przy użyciu polecenia comparer. Parametr elementSelector jest wywoływany dla każdego elementu w celu uzyskania elementu wyniku.

Dotyczy

GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza.

public:
generic <typename TSource, typename TKey, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TResult> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Linq::Expressions::Expression<Func<TKey, System::Collections::Generic::IEnumerable<TSource> ^, TResult> ^> ^ resultSelector);
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult>> resultSelector);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Linq.Expressions.Expression<Func<'Key, seq<'Source>, 'Result>> -> System.Linq.IQueryable<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TResult) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), resultSelector As Expression(Of Func(Of TKey, IEnumerable(Of TSource), TResult))) As IQueryable(Of TResult)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

TResult

Typ wartości wyniku zwracanej przez resultSelector.

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

resultSelector
Expression<Func<TKey,IEnumerable<TSource>,TResult>>

Funkcja tworzenia wartości wyniku z każdej grupy.

Zwraca

IQueryable<TResult>

Element T:System.Linq.IQueryable`1 , który ma argument TResult typu i gdzie każdy element reprezentuje projekcję dla grupy i jej klucza.

Wyjątki

source lub keySelectorresultSelector ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą metody GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) grupować 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.AsQueryable().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

Shared 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.AsQueryable().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 with this age: " & result.Count)
        output.AppendLine("Minimum age: " & result.Min)
        output.AppendLine("Maximum age: " & result.Max)
    Next

    ' Display the output.
    MsgBox(output.ToString())

    ' This code produces the following output:

    '  Age group: 8
    '  Number of pets with this age: 1
    '  Minimum age: 8.3
    '  Maximum age: 8.3

    '  Age group: 4
    '  Number of pets with this age: 2
    '  Minimum age: 4.3
    '  Maximum age: 4.9

    '  Age group: 1
    '  Number of pets with this age: 1
    '  Minimum age: 1.5
    '  Maximum age: 1.5
End Sub

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza, która jest uzyskiwana przez wywołanie keySelector każdego elementu. Parametr resultSelector służy do uzyskiwania wartości wyniku z każdej grupy i jej klucza.

Dotyczy

GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Grupuje elementy sekwencji zgodnie z określoną kluczową funkcją wyboru i tworzy wartość wyniku z każdej grupy i klucza. Klucze są porównywane przy użyciu określonego modułu porównującego.

public:
generic <typename TSource, typename TKey, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TResult> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Linq::Expressions::Expression<Func<TKey, System::Collections::Generic::IEnumerable<TSource> ^, TResult> ^> ^ resultSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Linq.IQueryable<TResult> GroupBy<TSource,TKey,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Linq.Expressions.Expression<Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult>> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Linq.Expressions.Expression<Func<'Key, seq<'Source>, 'Result>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Linq.IQueryable<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TResult) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), resultSelector As Expression(Of Func(Of TKey, IEnumerable(Of TSource), TResult)), comparer As IEqualityComparer(Of TKey)) As IQueryable(Of TResult)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

TResult

Typ wartości wyniku zwracanej przez resultSelector.

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

resultSelector
Expression<Func<TKey,IEnumerable<TSource>,TResult>>

Funkcja tworzenia wartości wyniku z każdej grupy.

comparer
IEqualityComparer<TKey>

Element IEqualityComparer<T> do porównywania kluczy.

Zwraca

IQueryable<TResult>

Element T:System.Linq.IQueryable`1 , który ma argument TResult typu i gdzie każdy element reprezentuje projekcję dla grupy i jej klucza.

Wyjątki

source lub keySelectorresultSelector lub comparer ma wartość null.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource, TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza, które są uzyskiwane przez wywołanie keySelector każdego elementu. Parametr comparer służy do porównywania kluczy, a resultSelector parametr służy do uzyskiwania wartości wynikowej z każdej grupy i jej klucza.

Dotyczy

GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<System::Linq::IGrouping<TKey, TSource> ^> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector);
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> -> System.Linq.IQueryable<System.Linq.IGrouping<'Key, 'Source>>
<Extension()>
Public Function GroupBy(Of TSource, TKey) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey))) As IQueryable(Of IGrouping(Of TKey, TSource))

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

Zwraca

IQueryable<IGrouping<TKey,TSource>>

Obiekt IQueryable<IGrouping<TKey, TSource>> w języku C# lub IQueryable(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.

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) grupować elementy sekwencji.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void GroupByEx1()
{
    // Create a list of Pet objects.
    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 Pet.Age as the key.
    // Use Pet.Name as the value for each entry.
    var query = pets.AsQueryable().GroupBy(pet => pet.Age);

    // Iterate over each IGrouping in the collection.
    foreach (var ageGroup in query)
    {
        Console.WriteLine("Age group: {0}  Number of pets: {1}", ageGroup.Key, ageGroup.Count());
    }
}

/*
    This code produces the following output:

    Age group: 8  Number of pets: 1
    Age group: 4  Number of pets: 2
    Age group: 1  Number of pets: 1

*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Shared 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 Pet.Age as the key.
    ' Use Pet.Name as the value for each entry.
    Dim query = pets.AsQueryable().GroupBy(Function(pet) pet.Age)

    Dim output As New System.Text.StringBuilder
    ' Iterate over each IGrouping in the collection.
    For Each ageGroup In query
        output.AppendFormat("Age group: {0}   Number of pets: {1}{2}", ageGroup.Key, ageGroup.Count(), vbCrLf)
    Next

    ' Display the output.
    MsgBox(output.ToString())
End Sub

' This code produces the following output:
'
' Age group: 8   Number of pets: 1
' Age group: 4   Number of pets: 2
' Age group: 1   Number of pets: 1

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza, która jest uzyskiwana przez wywołanie keySelector każdego elementu.

Dotyczy

GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Grupuje elementy sekwencji zgodnie z określoną funkcją selektora kluczy i porównuje klucze przy użyciu określonego modułu porównującego.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<System::Linq::IGrouping<TKey, TSource> ^> ^ GroupBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Linq.IQueryable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Collections.Generic.IEqualityComparer<'Key> -> System.Linq.IQueryable<System.Linq.IGrouping<'Key, 'Source>>
<Extension()>
Public Function GroupBy(Of TSource, TKey) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), comparer As IEqualityComparer(Of TKey)) As IQueryable(Of IGrouping(Of TKey, TSource))

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez funkcję reprezentowaną w keySelectorpliku .

Parametry

source
IQueryable<TSource>

Element, IQueryable<T> którego elementy do grupowania.

keySelector
Expression<Func<TSource,TKey>>

Funkcja wyodrębniania klucza do każdego elementu.

comparer
IEqualityComparer<TKey>

Element IEqualityComparer<T> do porównywania kluczy.

Zwraca

IQueryable<IGrouping<TKey,TSource>>

Element IQueryable<IGrouping<TKey, TSource>> w języku C# lub IQueryable(Of IGrouping(Of TKey, TSource)) Visual Basic, w którym każda z nich IGrouping<TKey,TElement> zawiera sekwencję obiektów i klucz.

Wyjątki

source lub keySelectorcomparer ma wartość null.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery<TElement>(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że grupuje elementy source według wartości klucza. Wartość klucza jest uzyskiwana przez wywołanie keySelector każdego elementu, a wartości klucza są porównywane przy użyciu polecenia comparer.

Dotyczy