Skip to content

Commit b87c0e4

Browse files
authored
SEO + other minor fixes (#6279)
1 parent e9abe3c commit b87c0e4

20 files changed

+759
-741
lines changed
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
---
2-
title: Create a nested group
3-
description: How to create a nested group.
2+
title: Create a nested group (LINQ in C#)
3+
description: Learn how to create a nested group in a LINQ query expression in C#.
44
ms.date: 12/1/2016
55
ms.assetid: e9f00708-362e-4d13-98c5-d77549347ba0
66
---
77
# Create a nested group
88

9-
The following example shows how to create nested groups in a LINQ query expression. Each group that is created according to student year or grade level is then further subdivided into groups based on the individuals' names.
10-
9+
The following example shows how to create nested groups in a LINQ query expression. Each group that is created according to student year or grade level is then further subdivided into groups based on the individuals' names.
10+
1111
## Example
1212

13-
> [!NOTE]
14-
> This example contains references to objects that are defined in the sample code in [Query a collection of objects](query-a-collection-of-objects.md).
13+
> [!NOTE]
14+
> This example contains references to objects that are defined in the sample code in [Query a collection of objects](query-a-collection-of-objects.md).
15+
16+
[!code-csharp[csProgGuideLINQ#24](~/samples/snippets/csharp/concepts/linq/how-to-create-a-nested-group_1.cs)]
17+
18+
Note that three nested `foreach` loops are required to iterate over the inner elements of a nested group.
19+
20+
## See also
1521

16-
[!code-csharp[csProgGuideLINQ#24](../../../samples/snippets/csharp/concepts/linq/how-to-create-a-nested-group_1.cs)]
17-
18-
Note that three nested `foreach` loops are required to iterate over the inner elements of a nested group.
19-
20-
## See also
21-
[LINQ Query Expressions](index.md)
22+
[Language Integrated Query (LINQ)](index.md)
Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
---
2-
title: Dynamically specify predicate filters at runtime
3-
description: How to dynamically specify predicate filters at runtime.
2+
title: Dynamically specify predicate filters at runtime (LINQ in C#)
3+
description: Learn how to dynamically specify predicate filters at runtime using LINQ in C#.
44
ms.date: 12/1/2016
55
ms.assetid: 90238470-0767-497c-916c-52d0d16845e0
66
---
77
# Dynamically specify predicate filters at runtime
88

9-
In some cases you do not know until run time how many predicates you have to apply to source elements in the `where` clause. One way to dynamically specify multiple predicate filters is to use the <xref:System.Linq.Enumerable.Contains%2A> method, as shown in the following example. The example is constructed in two ways. First, the project is run by filtering on values that are provided in the program. Then the project is run again by using input provided at run time.
10-
11-
## To filter by using the Contains method
12-
13-
1. Open a new console application and name it `PredicateFilters`.
14-
15-
2. Copy the `StudentClass` class from [Query a collection of objects](query-a-collection-of-objects.md) and paste it into namespace `PredicateFilters` underneath class `Program`. `StudentClass` provides a list of `Student` objects.
16-
17-
3. Comment out the `Main` method in `StudentClass`.
18-
19-
4. Replace class `Program` with the following code.
20-
21-
[!code-csharp[csProgGuideLINQ#26](../../../samples/snippets/csharp/concepts/linq/how-to-dynamically-specify-predicate-filters-at-runtime_1.cs)]
22-
23-
5. Add the following line to the `Main` method in class `DynamicPredicates`, under the declaration of `ids`.
24-
9+
In some cases, you don't know until run time how many predicates you have to apply to source elements in the `where` clause. One way to dynamically specify multiple predicate filters is to use the <xref:System.Linq.Enumerable.Contains%2A> method, as shown in the following example. The example is constructed in two ways. First, the project is run by filtering on values that are provided in the program. Then the project is run again by using input provided at run time.
10+
11+
## To filter by using the Contains method
12+
13+
1. Open a new console application and name it `PredicateFilters`.
14+
15+
2. Copy the `StudentClass` class from [Query a collection of objects](query-a-collection-of-objects.md) and paste it into namespace `PredicateFilters` underneath class `Program`. `StudentClass` provides a list of `Student` objects.
16+
17+
3. Comment out the `Main` method in `StudentClass`.
18+
19+
4. Replace class `Program` with the following code:
20+
21+
[!code-csharp[csProgGuideLINQ#26](~/samples/snippets/csharp/concepts/linq/how-to-dynamically-specify-predicate-filters-at-runtime_1.cs)]
22+
23+
5. Add the following line to the `Main` method in class `DynamicPredicates`, under the declaration of `ids`.
24+
2525
```csharp
2626
QueryById(ids);
2727
```
2828

29-
6. Run the project.
30-
31-
7. The following output is displayed in a console window:
32-
33-
Garcia: 114
34-
35-
O'Donnell: 112
36-
37-
Omelchenko: 111
38-
39-
8. The next step is to run the project again, this time by using input entered at run time instead of array `ids`. Change `QueryByID(ids)` to `QueryByID(args)` in the `Main` method.
40-
41-
9. Run the project with the command line arguments `122 117 120 115`. When the project is run, those values become elements of `args`, the parameter of the `Main` method..
42-
43-
10. The following output is displayed in a console window:
44-
45-
Adams: 120
46-
47-
Feng: 117
48-
49-
Garcia: 115
50-
51-
Tucker: 122
52-
53-
## To filter by using a switch statement
54-
55-
1. You can use a `switch` statement to select among predetermined alternative queries. In the following example, `studentQuery` uses a different `where` clause depending on which grade level, or year, is specified at run time.
56-
57-
2. Copy the following method and paste it into class `DynamicPredicates`.
58-
59-
[!code-csharp[csProgGuideLINQ#27](../../../samples/snippets/csharp/concepts/linq//how-to-dynamically-specify-predicate-filters-at-runtime_2.cs)]
60-
61-
3. In the `Main` method, replace the call to `QueryByID` with the following call, which sends the first element from the `args` array as its argument: `QueryByYear(args[0])`.
62-
63-
4. Run the project with a command line argument of an integer value between 1 and 4.
64-
65-
66-
## See Also
67-
[LINQ Query Expressions](index.md)
68-
[where clause](../language-reference/keywords/where-clause.md)
29+
6. Run the project.
30+
31+
7. The following output is displayed in a console window:
32+
33+
Garcia: 114
34+
35+
O'Donnell: 112
36+
37+
Omelchenko: 111
38+
39+
8. The next step is to run the project again, this time by using input entered at run time instead of array `ids`. Change `QueryByID(ids)` to `QueryByID(args)` in the `Main` method.
40+
41+
9. Run the project with the command line arguments `122 117 120 115`. When the project is run, those values become elements of `args`, the parameter of the `Main` method..
42+
43+
10. The following output is displayed in a console window:
44+
45+
Adams: 120
46+
47+
Feng: 117
48+
49+
Garcia: 115
50+
51+
Tucker: 122
52+
53+
## To filter by using a switch statement
54+
55+
1. You can use a `switch` statement to select among predetermined alternative queries. In the following example, `studentQuery` uses a different `where` clause depending on which grade level, or year, is specified at run time.
56+
57+
2. Copy the following method and paste it into class `DynamicPredicates`.
58+
59+
[!code-csharp[csProgGuideLINQ#27](~/samples/snippets/csharp/concepts/linq//how-to-dynamically-specify-predicate-filters-at-runtime_2.cs)]
60+
61+
3. In the `Main` method, replace the call to `QueryByID` with the following call, which sends the first element from the `args` array as its argument: `QueryByYear(args[0])`.
62+
63+
4. Run the project with a command line argument of an integer value between 1 and 4.
64+
65+
## See also
66+
67+
[Language Integrated Query (LINQ)](index.md)
68+
[where clause](../language-reference/keywords/where-clause.md)
Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,80 @@
11
---
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#.
44
ms.date: 12/1/2016
55
ms.assetid: 2e4ec27f-06fb-4de7-8973-0189906d4520
66
---
77
# Group query results
88

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

Comments
 (0)