Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions docs/csharp/linq/create-a-nested-group.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
---
title: Create a nested group
description: How to create a nested group.
title: Create a nested group (LINQ in C#)
description: Learn how to create a nested group in a LINQ query expression in C#.
ms.date: 12/1/2016
ms.assetid: e9f00708-362e-4d13-98c5-d77549347ba0
---
# Create a nested group

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

## Example

> [!NOTE]
> 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).
> [!NOTE]
> 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).

[!code-csharp[csProgGuideLINQ#24](~/samples/snippets/csharp/concepts/linq/how-to-create-a-nested-group_1.cs)]

Note that three nested `foreach` loops are required to iterate over the inner elements of a nested group.

## See also

[!code-csharp[csProgGuideLINQ#24](../../../samples/snippets/csharp/concepts/linq/how-to-create-a-nested-group_1.cs)]

Note that three nested `foreach` loops are required to iterate over the inner elements of a nested group.

## See also
[LINQ Query Expressions](index.md)
[Language Integrated Query (LINQ)](index.md)
116 changes: 58 additions & 58 deletions docs/csharp/linq/dynamically-specify-predicate-filters-at-runtime.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
---
title: Dynamically specify predicate filters at runtime
description: How to dynamically specify predicate filters at runtime.
title: Dynamically specify predicate filters at runtime (LINQ in C#)
description: Learn how to dynamically specify predicate filters at runtime using LINQ in C#.
ms.date: 12/1/2016
ms.assetid: 90238470-0767-497c-916c-52d0d16845e0
---
# Dynamically specify predicate filters at runtime

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.
## To filter by using the Contains method
1. Open a new console application and name it `PredicateFilters`.
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.
3. Comment out the `Main` method in `StudentClass`.
4. Replace class `Program` with the following code.
[!code-csharp[csProgGuideLINQ#26](../../../samples/snippets/csharp/concepts/linq/how-to-dynamically-specify-predicate-filters-at-runtime_1.cs)]
5. Add the following line to the `Main` method in class `DynamicPredicates`, under the declaration of `ids`.
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.

## To filter by using the Contains method

1. Open a new console application and name it `PredicateFilters`.

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.

3. Comment out the `Main` method in `StudentClass`.

4. Replace class `Program` with the following code:

[!code-csharp[csProgGuideLINQ#26](~/samples/snippets/csharp/concepts/linq/how-to-dynamically-specify-predicate-filters-at-runtime_1.cs)]

5. Add the following line to the `Main` method in class `DynamicPredicates`, under the declaration of `ids`.

```csharp
QueryById(ids);
```

6. Run the project.
7. The following output is displayed in a console window:
Garcia: 114
O'Donnell: 112
Omelchenko: 111
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.
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..
10. The following output is displayed in a console window:
Adams: 120
Feng: 117
Garcia: 115
Tucker: 122
## To filter by using a switch statement
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.
2. Copy the following method and paste it into class `DynamicPredicates`.
[!code-csharp[csProgGuideLINQ#27](../../../samples/snippets/csharp/concepts/linq//how-to-dynamically-specify-predicate-filters-at-runtime_2.cs)]
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])`.
4. Run the project with a command line argument of an integer value between 1 and 4.
## See Also
[LINQ Query Expressions](index.md)
[where clause](../language-reference/keywords/where-clause.md)
6. Run the project.

7. The following output is displayed in a console window:

Garcia: 114

O'Donnell: 112

Omelchenko: 111

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.

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

10. The following output is displayed in a console window:

Adams: 120

Feng: 117

Garcia: 115

Tucker: 122

## To filter by using a switch statement

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.

2. Copy the following method and paste it into class `DynamicPredicates`.

[!code-csharp[csProgGuideLINQ#27](~/samples/snippets/csharp/concepts/linq//how-to-dynamically-specify-predicate-filters-at-runtime_2.cs)]

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])`.

4. Run the project with a command line argument of an integer value between 1 and 4.

## See also

[Language Integrated Query (LINQ)](index.md)
[where clause](../language-reference/keywords/where-clause.md)
141 changes: 74 additions & 67 deletions docs/csharp/linq/group-query-results.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,80 @@
---
title: Group query results
description: How to group results.
title: Group query results (LINQ in C#)
description: Learn how to group results using LINQ in C#.
ms.date: 12/1/2016
ms.assetid: 2e4ec27f-06fb-4de7-8973-0189906d4520
---
# Group query results

Grouping is one of the most powerful capabilities of LINQ. The following examples show how to group data in various ways:

- By a single property.

- By the first letter of a string property.

- By a computed numeric range.

- By Boolean predicate or other expression.

- By a compound key.

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

## Example
All the examples in this topic use the following helper classes and data sources.

[!code-csharp[csProgGuideLINQ#15](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_1.cs)]

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

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySingleProperty()`.

[!code-csharp[csProgGuideLINQ#17](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_2.cs)]

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

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySubstring()`.

[!code-csharp[csProgGuideLINQ#18](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_3.cs)]

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

[!code-csharp[csProgGuideLINQ#50](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_4.cs)]

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByRange()`.

[!code-csharp[csProgGuideLINQ#19](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_5.cs)]

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

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByBoolean()`.

[!code-csharp[csProgGuideLINQ#20](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_6.cs)]

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

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByCompositeKey()`.

[!code-csharp[csProgGuideLINQ#21](../../../samples/snippets/csharp/concepts/linq/how-to-group-query-results_7.cs)]

## See also
<xref:System.Linq.Enumerable.GroupBy%2A>
<xref:System.Linq.IGrouping%602>
[LINQ Query Expressions](index.md)
[group clause](../language-reference/keywords/group-clause.md)
[Anonymous Types](../programming-guide/classes-and-structs/anonymous-types.md)
[Perform a Subquery on a Grouping Operation](perform-a-subquery-on-a-grouping-operation.md)
[Create a Nested Group](create-a-nested-group.md)
[Grouping Data](../programming-guide/concepts/linq/grouping-data.md)
Grouping is one of the most powerful capabilities of LINQ. The following examples show how to group data in various ways:

- By a single property.

- By the first letter of a string property.

- By a computed numeric range.

- By Boolean predicate or other expression.

- By a compound key.

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

## Example

All the examples in this topic use the following helper classes and data sources.

[!code-csharp[csProgGuideLINQ#15](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_1.cs)]

## Example

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.

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySingleProperty()`.

[!code-csharp[csProgGuideLINQ#17](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_2.cs)]

## Example

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.

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupBySubstring()`.

[!code-csharp[csProgGuideLINQ#18](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_3.cs)]

## Example

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.

[!code-csharp[csProgGuideLINQ#50](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_4.cs)]

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByRange()`.

[!code-csharp[csProgGuideLINQ#19](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_5.cs)]

## Example

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.

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByBoolean()`.

[!code-csharp[csProgGuideLINQ#20](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_6.cs)]

## Example

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.

Paste the following method into the `StudentClass` class. Change the calling statement in the `Main` method to `sc.GroupByCompositeKey()`.

[!code-csharp[csProgGuideLINQ#21](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_7.cs)]

## See also

<xref:System.Linq.Enumerable.GroupBy%2A>
<xref:System.Linq.IGrouping%602>
[Language Integrated Query (LINQ)](index.md)
[group clause](../language-reference/keywords/group-clause.md)
[Anonymous Types](../programming-guide/classes-and-structs/anonymous-types.md)
[Perform a Subquery on a Grouping Operation](perform-a-subquery-on-a-grouping-operation.md)
[Create a Nested Group](create-a-nested-group.md)
[Grouping Data](../programming-guide/concepts/linq/grouping-data.md)
Loading