Bagikan melalui


Grouping Data

Grouping refers to the operation of putting data into groups so that the elements in each group share a common attribute.

The following illustration shows the results of grouping a sequence of characters. The key for each group is the character.

LINQ Grouping Operations

The standard query operator methods that group data elements are listed in the following section.

Methods

Method Name

Description

C# Query Expression Syntax

Visual Basic Query Expression Syntax

More Information

GroupBy

Groups elements that share a common attribute. Each group is represented by an IGrouping<TKey, TElement> object.

group … by

-or-

group … by … into …

Group … By … Into …

Enumerable.GroupBy

Queryable.GroupBy

ToLookup

Inserts elements into a Lookup<TKey, TElement> (a one-to-many dictionary) based on a key selector function.

Not applicable.

Not applicable.

Enumerable.ToLookup

Query Expression Syntax Example

The following code example uses the group by clause in C# or the Group By clause in Visual Basic to group integers in a list according to whether they are even or odd.

Dim numbers As New System.Collections.Generic.List(Of Integer)(
             New Integer() {35, 44, 200, 84, 3987, 4, 199, 329, 446, 208})

        Dim query = From number In numbers 
                    Group By Remainder = (number Mod 2) Into Group

        Dim sb As New System.Text.StringBuilder()
        For Each group In query
            sb.AppendLine(If(group.Remainder = 0, vbCrLf & "Even numbers:", vbCrLf & "Odd numbers:"))
            For Each num In group.Group
                sb.AppendLine(num)
            Next 
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' Odd numbers: 
        ' 35 
        ' 3987 
        ' 199 
        ' 329 

        ' Even numbers: 
        ' 44 
        ' 200 
        ' 84 
        ' 4 
        ' 446 
        ' 208
            List<int> numbers = new List<int>() { 35, 44, 200, 84, 3987, 4, 199, 329, 446, 208 };

            IEnumerable<IGrouping<int, int>> query = from number in numbers
                                                     group number by number % 2;

            foreach (var group in query)
            {
                Console.WriteLine(group.Key == 0 ? "\nEven numbers:" : "\nOdd numbers:");
                foreach (int i in group)
                    Console.WriteLine(i);
            }

            /* This code produces the following output:

                Odd numbers:
                35
                3987
                199
                329

                Even numbers:
                44
                200
                84
                4
                446
                208
            */

See Also

Tasks

How to: Create a Nested Group (C# Programming Guide)

How to: Group Files by Extension (LINQ)

How to: Group Query Results (C# Programming Guide)

How to: Perform a Subquery on a Grouping Operation (C# Programming Guide)

How to: Split a File Into Many Files by Using Groups (LINQ)

Reference

group clause (C# Reference)

Group By Clause (Visual Basic)

System.Linq

Concepts

Standard Query Operators Overview