Читати англійською Редагувати

Поділитися через


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

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

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

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

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

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Keys are compared by using a specified comparer and the elements of each group are projected by using a specified function.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

TElement

The type of the elements in each IGrouping<TKey,TElement>.

TResult

The type of the result value returned by resultSelector.

Parameters

source
IQueryable<TSource>

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

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>

An IEqualityComparer<T> to compare keys.

Returns

IQueryable<TResult>

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

source or keySelector or elementSelector or resultSelector or comparer is null.

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it groups the elements of source by key values that are obtained by invoking keySelector on each element. The comparer parameter is used to compare key values. 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.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The elements of each group are projected by using a specified function.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

TElement

The type of the elements in each IGrouping<TKey,TElement>.

TResult

The type of the result value returned by resultSelector.

Parameters

source
IQueryable<TSource>

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

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>

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

source or keySelector or elementSelector or resultSelector is null.

Examples

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.

C#
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
    */
}

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it groups the elements of source by key values that are obtained by invoking keySelector on each element. 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.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

TElement

The type of the elements in each IGrouping<TKey,TElement>.

Parameters

source
IQueryable<TSource>

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

A function to map each source element to an element in an IGrouping<TKey,TElement>.

comparer
IEqualityComparer<TKey>

An IEqualityComparer<T> to compare keys.

Returns

IQueryable<IGrouping<TKey,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

source or keySelector or elementSelector or comparer is null.

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector on each element. Key values are compared by using comparer. The elementSelector parameter is invoked on each element to obtain a result element.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

TElement

The type of the elements in each IGrouping<TKey,TElement>.

Parameters

source
IQueryable<TSource>

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

A function to map each source element to an element in an IGrouping<TKey,TElement>.

Returns

IQueryable<IGrouping<TKey,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

source or keySelector or elementSelector is null.

Examples

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.

C#
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
*/

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector on each element. It invokes elementSelector on each element to obtain a result element.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

TResult

The type of the result value returned by resultSelector.

Parameters

source
IQueryable<TSource>

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>

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

source or keySelector or resultSelector is null.

Examples

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.

C#
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
    */
}

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it groups the elements of source by a key value that is obtained by invoking keySelector on each element. The resultSelector parameter is used to obtain a result value from each group and its key.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Keys are compared by using a specified comparer.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

TResult

The type of the result value returned by resultSelector.

Parameters

source
IQueryable<TSource>

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>

An IEqualityComparer<T> to compare keys.

Returns

IQueryable<TResult>

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

source or keySelector or resultSelector or comparer is null.

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it groups the elements of source by key values that are obtained by invoking keySelector on each element. 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.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

Parameters

source
IQueryable<TSource>

An IQueryable<T> whose elements to group.

keySelector
Expression<Func<TSource,TKey>>

A function to extract the key for each element.

comparer
IEqualityComparer<TKey>

An IEqualityComparer<T> to compare keys.

Returns

IQueryable<IGrouping<TKey,TSource>>

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

source or keySelector or comparer is null.

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it groups the elements of source by a key value. The key value is obtained by invoking keySelector on each element, and key values are compared by using comparer.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Groups the elements of a sequence according to a specified key selector function.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the key returned by the function represented in keySelector.

Parameters

source
IQueryable<TSource>

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

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 or keySelector is null.

Examples

The following code example demonstrates how to use GroupBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) to group the elements of a sequence.

C#
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

*/

Remarks

This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

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. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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

.NET 10 та інші версії
Продукт Версії
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0