Skip to content

GroupBy

Eugene Sadovoi edited this page Jul 18, 2016 · 5 revisions

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

Syntax

GroupBy(keySelector[, elementSelector[, resultSelector]])

Parameters

keySelector

A function to extract the key for each element:
TKey keySelector(TSource)

elementSelector

A function to map each source element to an element in an grouping:
TElement elementSelector(TSource)

resultSelector

A function to create a result value from each group:
TResult resultSelector(TKey, Iterable<TElement>)

Return Value

A collection of elements of type TResult where each element represents a projection over a group and its key.

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated. The GroupBy method returns a collection of grouping of TKey, TElement objects, one for each distinct key that was encountered. A TKey, TElement grouping is an Iterable<T> that also has a key associated with its elements.

Example

The following code example demonstrates how to use GroupBy to group the projected elements of a sequence.

class Pet {
    constructor(name, age) {
        this.Name = name;
        this.Age = age;
    }
}

var petsList = [ new Pet("Barley", 8.3 ),
                 new Pet("Boots", 4.9 ),
                 new Pet("Whiskers", 1.5 ),
                 new Pet("Daisy", 4.3 ) ];

// Create enumerable and 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 pets = Enumerable.asEnumerable(petsList);
var query = pets.GroupBy( pet => Math.floor(pet.Age), pet => pet.Age, (baseAge, ages) => { 
                          return { Key: baseAge, 
                                   Count: ages.length, 
                                   Min: Enumerable.asEnumerable(ages).Min(), 
                                   Max: Enumerable.asEnumerable(ages).Max() }});

// Iterate over each anonymous type.
for (var result of query)
{
    console.log("Age group: " + result.Key);
    console.log("Number of pets in this age group: " + result.Count);
    console.log("Minimum age: " + result.Min);
    console.log("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
*/

Try it out

Clone this wiki locally