Queryable.GroupBy Queryable.GroupBy Queryable.GroupBy Queryable.GroupBy Method

Definition

シーケンスの要素をグループ化します。 Groups the elements of a sequence.

Overloads

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. キーの比較には、指定された比較子を使用し、各グループの要素の射影には、指定された関数を使用します。 Keys are compared by using a specified comparer and the elements of each group are projected by using a specified function.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 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<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された関数を使用して各グループの要素を射影します。 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<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>)

指定された関数を使用して、シーケンスの要素をグループ化し、各グループの要素を射影します。 Groups the elements of a sequence and projects the elements for each group by using a specified function. キー値の比較には、指定された比較子を使用します。 Key values are compared by using a specified comparer.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 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<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. キーの比較には、指定された比較子を使用します。 Keys are compared by using a specified comparer.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された比較子を使用してキーを比較します。 Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化します。 Groups the elements of a sequence according to a specified key selector function.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. キーの比較には、指定された比較子を使用し、各グループの要素の射影には、指定された関数を使用します。 Keys are compared by using a specified comparer and the elements of each group are projected by using a specified function.

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);
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)

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

TElement

IGrouping<TKey,TElement> の要素の型。 The type of the elements in each IGrouping<TKey,TElement>.

TResult

によって返される結果値の型resultSelectorします。 The type of the result value returned by resultSelector.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

elementSelector
Expression<Func<TSource,TElement>>

ソースの各要素を IGrouping<TKey,TElement> の要素に割り当てる関数。 A function to map each source element to an element in an IGrouping<TKey,TElement>.

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

各グループから結果値を作成する関数。 A function to create a result value from each group.

comparer
IEqualityComparer<TKey>

キーを比較する IEqualityComparer<T> An IEqualityComparer<T> to compare keys.

Returns

IQueryable<TResult>

TResult の型引数を持つ T:System.Linq.IQueryable`1。各要素は、グループとそのキーの射影を表します。 An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a group and its key.

Exceptions

sourcekeySelectorelementSelectorresultSelector、または comparernull です。 source or keySelector or elementSelector or resultSelector or comparer is null.

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>, IEqualityComparer<TKey>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceを呼び出して取得するキー値によってkeySelector各要素に対して。The expected behavior is that it groups the elements of source by key values that are obtained by invoking keySelector on each element. comparerパラメーターを使用してキーの値を比較します。The comparer parameter is used to compare key values. elementSelectorパラメーターは、各グループの要素をプロジェクトに使用し、resultSelectorパラメーターは各グループとそのキーから結果値を取得するために使用します。The elementSelector parameter is used to project the elements of each group, and the resultSelector parameter is used to obtain a result value from each group and its key.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 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 <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)

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

TElement

IGrouping<TKey,TElement> の要素の型。 The type of the elements in each IGrouping<TKey,TElement>.

TResult

によって返される結果値の型resultSelectorします。 The type of the result value returned by resultSelector.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

elementSelector
Expression<Func<TSource,TElement>>

ソースの各要素を IGrouping<TKey,TElement> の要素に割り当てる関数。 A function to map each source element to an element in an IGrouping<TKey,TElement>.

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

各グループから結果値を作成する関数。 A function to create a result value from each group.

Returns

IQueryable<TResult>

TResult の型引数を持つ T:System.Linq.IQueryable`1。各要素は、グループとそのキーの射影を表します。 An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a group and its key.

Exceptions

sourcekeySelectorelementSelector、または resultSelectornull です。 source or keySelector or elementSelector or resultSelector is null.

Examples

次のコード例は、使用する方法を示しますGroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>)シーケンスの要素をグループ化し、プロジェクトの種類の結果のシーケンスTResultします。The following code example demonstrates how to use GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>) to group the elements of a sequence and project a sequence of results of type 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

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey,TElement,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, Expression<Func<TKey,IEnumerable<TElement>,TResult>>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceを呼び出して取得するキー値によってkeySelector各要素に対して。The expected behavior is that it groups the elements of source by key values that are obtained by invoking keySelector on each element. elementSelectorパラメーターは、各グループの要素をプロジェクトに使用し、resultSelectorパラメーターは各グループとそのキーから結果値を取得するために使用します。The elementSelector parameter is used to project the elements of each group, and the resultSelector parameter is used to obtain a result value from each group and its key.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された関数を使用して各グループの要素を射影します。 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 <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))

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

TElement

IGrouping<TKey,TElement> の要素の型。 The type of the elements in each IGrouping<TKey,TElement>.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

elementSelector
Expression<Func<TSource,TElement>>

ソースの各要素を IGrouping<TKey,TElement> の要素に割り当てる関数。 A function to map each source element to an element in an IGrouping<TKey,TElement>.

Returns

IQueryable<IGrouping<TKey,TElement>>

C# では IQueryable<IGrouping<TKey, TElement>>、Visual Basic では IQueryable(Of IGrouping(Of TKey, TElement))。ここでは、各 IGrouping<TKey,TElement> に、TElement 型のオブジェクトのシーケンスとキーが含まれています。 An IQueryable<IGrouping<TKey, TElement>> in C# or IQueryable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each IGrouping<TKey,TElement> contains a sequence of objects of type TElement and a key.

Exceptions

sourcekeySelector、または elementSelector は、null です。 source or keySelector or elementSelector is null.

Examples

次のコード例は、使用する方法を示しますGroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>)シーケンスの要素をグループ化します。The following code example demonstrates how to use GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) to group the elements of a sequence.

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

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceを呼び出して取得するキー値によってkeySelector各要素に対して。The expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector on each element. 呼び出すelementSelector結果の要素を取得するには、各要素に対して。It invokes elementSelector on each element to obtain a result element.

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

指定された関数を使用して、シーケンスの要素をグループ化し、各グループの要素を射影します。 Groups the elements of a sequence and projects the elements for each group by using a specified function. キー値の比較には、指定された比較子を使用します。 Key values are compared by using a specified comparer.

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);
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))

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

TElement

IGrouping<TKey,TElement> の要素の型。 The type of the elements in each IGrouping<TKey,TElement>.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

elementSelector
Expression<Func<TSource,TElement>>

ソースの各要素を IGrouping<TKey,TElement> の要素に割り当てる関数。 A function to map each source element to an element in an IGrouping<TKey,TElement>.

comparer
IEqualityComparer<TKey>

キーを比較する IEqualityComparer<T> An IEqualityComparer<T> to compare keys.

Returns

IQueryable<IGrouping<TKey,TElement>>

C# では IQueryable<IGrouping<TKey, TElement>>、Visual Basic では IQueryable(Of IGrouping(Of TKey, TElement))。ここでは、各 IGrouping<TKey,TElement> に、TElement 型のオブジェクトのシーケンスとキーが含まれています。 An IQueryable<IGrouping<TKey, TElement>> in C# or IQueryable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each IGrouping<TKey,TElement> contains a sequence of objects of type TElement and a key.

Exceptions

sourcekeySelectorelementSelector、または comparernull です。 source or keySelector or elementSelector or comparer is null.

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>, IEqualityComparer<TKey>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceを呼び出して取得するキー値によってkeySelector各要素に対して。The expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector on each element. 使用してキー値の比較はcomparerします。Key values are compared by using comparer. elementSelectorパラメーターは、結果の要素を取得するには、各要素に対して呼び出されます。The elementSelector parameter is invoked on each element to obtain a result element.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 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 <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)

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

TResult

によって返される結果値の型resultSelectorします。 The type of the result value returned by resultSelector.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

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

各グループから結果値を作成する関数。 A function to create a result value from each group.

Returns

IQueryable<TResult>

TResult の型引数を持つ T:System.Linq.IQueryable`1。各要素は、グループとそのキーの射影を表します。 An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a group and its key.

Exceptions

sourcekeySelector、または resultSelector は、null です。 source or keySelector or resultSelector is null.

Examples

次のコード例は、使用する方法を示しますGroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>)シーケンスの要素をグループ化し、プロジェクトの種類の結果のシーケンスTResultします。The following code example demonstrates how to use GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) to group the elements of a sequence and project a sequence of results of type 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

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceを呼び出して取得するキー値によってkeySelector各要素に対して。The expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector on each element. resultSelectorパラメーターは各グループとそのキーから結果値を取得するために使用します。The resultSelector parameter is used to obtain a result value from each group and its key.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. キーの比較には、指定された比較子を使用します。 Keys are compared by using a specified comparer.

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);
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)

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

TResult

によって返される結果値の型resultSelectorします。 The type of the result value returned by resultSelector.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

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

各グループから結果値を作成する関数。 A function to create a result value from each group.

comparer
IEqualityComparer<TKey>

キーを比較する IEqualityComparer<T> An IEqualityComparer<T> to compare keys.

Returns

IQueryable<TResult>

TResult の型引数を持つ T:System.Linq.IQueryable`1。各要素は、グループとそのキーの射影を表します。 An T:System.Linq.IQueryable`1 that has a type argument of TResult and where each element represents a projection over a group and its key.

Exceptions

sourcekeySelectorresultSelector、または comparernull です。 source or keySelector or resultSelector or comparer is null.

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey,TResult>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TKey,IEnumerable<TSource>,TResult>>, IEqualityComparer<TKey>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceを呼び出して取得するキー値によってkeySelector各要素に対して。The expected behavior is that it groups the elements of source by key values that are obtained by invoking keySelector on each element. comparerキーを比較するパラメーターを使用し、resultSelectorパラメーターは各グループとそのキーから結果値を取得するために使用します。The comparer parameter is used to compare keys and the resultSelector parameter is used to obtain a result value from each group and its key.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された比較子を使用してキーを比較します。 Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.

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);
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))

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

comparer
IEqualityComparer<TKey>

キーを比較する IEqualityComparer<T> An IEqualityComparer<T> to compare keys.

Returns

IQueryable<IGrouping<TKey,TSource>>

C# では IQueryable<IGrouping<TKey, TSource>>、Visual Basic では IQueryable(Of IGrouping(Of TKey, TSource))。ここでは、各 IGrouping<TKey,TElement> に、オブジェクトのシーケンスとキーが含まれています。 An IQueryable<IGrouping<TKey, TSource>> in C# or IQueryable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each IGrouping<TKey,TElement> contains a sequence of objects and a key.

Exceptions

sourcekeySelector、または comparer は、null です。 source or keySelector or comparer is null.

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceをキー値。The expected behavior is that it groups the elements of source by a key value. 呼び出して、キーの値を取得keySelector、各要素を使用してキー値の比較、およびcomparerします。The key value is obtained by invoking keySelector on each element, and key values are compared by using comparer.

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化します。 Groups the elements of a sequence according to a specified key selector function.

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))

Type Parameters

TSource

要素の型ソースします。 The type of the elements of source.

TKey

表された関数によって返されるキーの種類keySelectorします。 The type of the key returned by the function represented in keySelector.

パラメーター

source
IQueryable<TSource>

グループ化する要素を含む IQueryable<T> An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

各要素のキーを抽出する関数。 A function to extract the key for each element.

Returns

IQueryable<IGrouping<TKey,TSource>>

C# では IQueryable<IGrouping<TKey, TSource>>、Visual Basic では IQueryable(Of IGrouping(Of TKey, TSource))。ここでは、各 IGrouping<TKey,TElement> オブジェクトに、オブジェクトのシーケンスとキーが含まれています。 An IQueryable<IGrouping<TKey, TSource>> in C# or IQueryable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each IGrouping<TKey,TElement> object contains a sequence of objects and a key.

Exceptions

source または keySelectornull です。 source or keySelector is null.

Examples

次のコード例は、使用する方法を示しますGroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)シーケンスの要素をグループ化します。The following code example demonstrates how to use GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) to group the elements of a sequence.

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

Remarks

このメソッドは、少なくとも 1 つのパラメーター型のExpression<TDelegate>型引数は、のいずれか、Func<T,TResult>型。This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. ラムダ式で、これらのパラメーターを渡すことができますにコンパイルされると、Expression<TDelegate>します。For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)メソッドを生成、MethodCallExpression呼び出しを表すGroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)として構築されたジェネリック メソッド自体。The GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) method generates a MethodCallExpression that represents calling GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) itself as a constructed generic method. これは、後、渡します、MethodCallExpressionCreateQuery<TElement>(Expression)のメソッド、IQueryProviderによって表される、Providerのプロパティ、sourceパラメーター。It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

呼び出し元を表す式ツリーを実行した結果として発生するクエリの動作GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)の型の実装によって異なります、sourceパラメーター。The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) depends on the implementation of the type of the source parameter. 想定される動作の要素をグループ化sourceを呼び出して取得するキー値によってkeySelector各要素に対して。The expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector on each element.

Applies to