| 
1 | 1 | ---  | 
2 |  | -title: Group query results  | 
3 |  | -description: How to group results.  | 
 | 2 | +title: Group query results (LINQ in C#)  | 
 | 3 | +description: Learn how to group results using LINQ in C#.  | 
4 | 4 | ms.date: 12/1/2016  | 
5 | 5 | ms.assetid: 2e4ec27f-06fb-4de7-8973-0189906d4520  | 
6 | 6 | ---  | 
7 | 7 | # Group query results  | 
8 | 8 | 
 
  | 
9 |  | -Grouping is one of the most powerful capabilities of LINQ. The following examples show how to group data in various ways:    | 
10 |  | -    | 
11 |  | --   By a single property.    | 
12 |  | -    | 
13 |  | --   By the first letter of a string property.    | 
14 |  | -    | 
15 |  | --   By a computed numeric range.    | 
16 |  | -    | 
17 |  | --   By Boolean predicate or other expression.    | 
18 |  | -    | 
19 |  | --   By a compound key.    | 
20 |  | -    | 
21 |  | - In addition, the last two queries project their results into a new anonymous type that contains only the student's first and last name. For more information, see the [group clause](../language-reference/keywords/group-clause.md).    | 
22 |  | -    | 
23 |  | -## Example    | 
24 |  | - All the examples in this topic use the following helper classes and data sources.    | 
25 |  | -    | 
26 |  | - [!code-csharp[csProgGuideLINQ#15](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_1.cs)]    | 
27 |  | -    | 
28 |  | -## Example    | 
29 |  | - The following example shows how to group source elements by using a single property of the element as the group key. In this case the key is a `string`, the student's last name. It is also possible to use a substring for the key. The grouping operation uses the default equality comparer for the type.    | 
30 |  | -    | 
31 |  | - Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySingleProperty()`.    | 
32 |  | -    | 
33 |  | - [!code-csharp[csProgGuideLINQ#17](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_2.cs)]    | 
34 |  | -    | 
35 |  | -## Example    | 
36 |  | - The following example shows how to group source elements by using something other than a property of the object for the group key. In this example, the key is the first letter of the student's last name.    | 
37 |  | -    | 
38 |  | - Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySubstring()`.    | 
39 |  | -    | 
40 |  | - [!code-csharp[csProgGuideLINQ#18](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_3.cs)]    | 
41 |  | -    | 
42 |  | -## Example    | 
43 |  | - The following example shows how to group source elements by using a numeric range as a group key. The query then projects the results into an anonymous type that contains only the first and last name and the percentile range to which the student belongs. An anonymous type is used because it is not necessary to use the complete `Student` object to display the results. `GetPercentile` is a helper function that calculates a percentile based on the student's average score. The method returns an integer between 0 and 10.    | 
44 |  | -    | 
45 |  | - [!code-csharp[csProgGuideLINQ#50](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_4.cs)]    | 
46 |  | -    | 
47 |  | - Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByRange()`.    | 
48 |  | -    | 
49 |  | - [!code-csharp[csProgGuideLINQ#19](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_5.cs)]    | 
50 |  | -    | 
51 |  | -## Example    | 
52 |  | - The following example shows how to group source elements by using a Boolean comparison expression. In this example, the Boolean expression tests whether a student's average exam score is greater than 75. As in previous examples, the results are projected into an anonymous type because the complete source element is not needed. Note that the properties in the anonymous type become properties on the `Key` member and can be accessed by name when the query is executed.    | 
53 |  | -    | 
54 |  | - Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByBoolean()`.    | 
55 |  | -    | 
56 |  | - [!code-csharp[csProgGuideLINQ#20](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_6.cs)]    | 
57 |  | -    | 
58 |  | -## Example    | 
59 |  | - The following example shows how to use an anonymous type to encapsulate a key that contains multiple values. In this example, the first key value is the first letter of the student's last name. The second key value is a Boolean that specifies whether the student scored over 85 on the first exam. You can order the groups by any property in the key.    | 
60 |  | -    | 
61 |  | - Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByCompositeKey()`.    | 
62 |  | -    | 
63 |  | - [!code-csharp[csProgGuideLINQ#21](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_7.cs)]    | 
64 |  | -    | 
65 |  | -## See also    | 
66 |  | - <xref:System.Linq.Enumerable.GroupBy%2A>    | 
67 |  | - <xref:System.Linq.IGrouping%602>    | 
68 |  | - [LINQ Query Expressions](index.md)    | 
69 |  | - [group clause](../language-reference/keywords/group-clause.md)    | 
70 |  | - [Anonymous Types](../programming-guide/classes-and-structs/anonymous-types.md)    | 
71 |  | - [Perform a Subquery on a Grouping Operation](perform-a-subquery-on-a-grouping-operation.md)    | 
72 |  | - [Create a Nested Group](create-a-nested-group.md)    | 
73 |  | - [Grouping Data](../programming-guide/concepts/linq/grouping-data.md)  | 
 | 9 | +Grouping is one of the most powerful capabilities of LINQ. The following examples show how to group data in various ways:  | 
 | 10 | + | 
 | 11 | +- By a single property.  | 
 | 12 | + | 
 | 13 | +- By the first letter of a string property.  | 
 | 14 | + | 
 | 15 | +- By a computed numeric range.  | 
 | 16 | + | 
 | 17 | +- By Boolean predicate or other expression.  | 
 | 18 | + | 
 | 19 | +- By a compound key.  | 
 | 20 | + | 
 | 21 | +In addition, the last two queries project their results into a new anonymous type that contains only the student's first and last name. For more information, see the [group clause](../language-reference/keywords/group-clause.md).  | 
 | 22 | + | 
 | 23 | +## Example  | 
 | 24 | + | 
 | 25 | +All the examples in this topic use the following helper classes and data sources.  | 
 | 26 | + | 
 | 27 | +[!code-csharp[csProgGuideLINQ#15](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_1.cs)]  | 
 | 28 | + | 
 | 29 | +## Example  | 
 | 30 | + | 
 | 31 | +The following example shows how to group source elements by using a single property of the element as the group key. In this case the key is a `string`, the student's last name. It is also possible to use a substring for the key. The grouping operation uses the default equality comparer for the type.  | 
 | 32 | + | 
 | 33 | +Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySingleProperty()`.  | 
 | 34 | + | 
 | 35 | +[!code-csharp[csProgGuideLINQ#17](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_2.cs)]  | 
 | 36 | + | 
 | 37 | +## Example  | 
 | 38 | + | 
 | 39 | +The following example shows how to group source elements by using something other than a property of the object for the group key. In this example, the key is the first letter of the student's last name.  | 
 | 40 | + | 
 | 41 | +Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySubstring()`.  | 
 | 42 | + | 
 | 43 | +[!code-csharp[csProgGuideLINQ#18](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_3.cs)]  | 
 | 44 | + | 
 | 45 | +## Example  | 
 | 46 | + | 
 | 47 | +The following example shows how to group source elements by using a numeric range as a group key. The query then projects the results into an anonymous type that contains only the first and last name and the percentile range to which the student belongs. An anonymous type is used because it is not necessary to use the complete `Student` object to display the results. `GetPercentile` is a helper function that calculates a percentile based on the student's average score. The method returns an integer between 0 and 10.  | 
 | 48 | + | 
 | 49 | +[!code-csharp[csProgGuideLINQ#50](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_4.cs)]  | 
 | 50 | + | 
 | 51 | +Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByRange()`.  | 
 | 52 | + | 
 | 53 | +[!code-csharp[csProgGuideLINQ#19](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_5.cs)]  | 
 | 54 | + | 
 | 55 | +## Example  | 
 | 56 | + | 
 | 57 | +The following example shows how to group source elements by using a Boolean comparison expression. In this example, the Boolean expression tests whether a student's average exam score is greater than 75. As in previous examples, the results are projected into an anonymous type because the complete source element is not needed. Note that the properties in the anonymous type become properties on the `Key` member and can be accessed by name when the query is executed.  | 
 | 58 | + | 
 | 59 | +Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByBoolean()`.  | 
 | 60 | + | 
 | 61 | +[!code-csharp[csProgGuideLINQ#20](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_6.cs)]  | 
 | 62 | + | 
 | 63 | +## Example  | 
 | 64 | + | 
 | 65 | +The following example shows how to use an anonymous type to encapsulate a key that contains multiple values. In this example, the first key value is the first letter of the student's last name. The second key value is a Boolean that specifies whether the student scored over 85 on the first exam. You can order the groups by any property in the key.  | 
 | 66 | + | 
 | 67 | +Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByCompositeKey()`.  | 
 | 68 | + | 
 | 69 | +[!code-csharp[csProgGuideLINQ#21](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_7.cs)]  | 
 | 70 | + | 
 | 71 | +## See also  | 
 | 72 | + | 
 | 73 | +<xref:System.Linq.Enumerable.GroupBy%2A>    | 
 | 74 | +<xref:System.Linq.IGrouping%602>    | 
 | 75 | +[Language Integrated Query (LINQ)](index.md)    | 
 | 76 | +[group clause](../language-reference/keywords/group-clause.md)    | 
 | 77 | +[Anonymous Types](../programming-guide/classes-and-structs/anonymous-types.md)    | 
 | 78 | +[Perform a Subquery on a Grouping Operation](perform-a-subquery-on-a-grouping-operation.md)    | 
 | 79 | +[Create a Nested Group](create-a-nested-group.md)    | 
 | 80 | +[Grouping Data](../programming-guide/concepts/linq/grouping-data.md)    | 
0 commit comments