Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
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
28 changes: 28 additions & 0 deletions 101-linq-samples/docs/aggregates-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# LINQ - Aggregators

*Aggregator* methods return a single value calculated from all the elements in a sequence. the aggregator methods include `Count`, `Sum`, `Min`, `Max`, `Average`, and `Aggregate`.

## Sum all numeric elements in a sequence

This sample uses `Sum` to get the total of the numbers in an array.

``` cs --region sum-syntax --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Sum a projection from a sequence

This sample uses `Sum` to get the total number of characters of all words in the array.

``` cs --region sum-of-projection --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Sum all elements that are members of a group

This sample uses `Sum` to get the total units in stock for each product category.

``` cs --region grouped-sum --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Aggregates-Min »](./aggregates-2.md) Previous: [Aggregates »](./aggregates.md)**

**[Home](../README.md)**
35 changes: 35 additions & 0 deletions 101-linq-samples/docs/aggregates-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# LINQ - Aggregators

*Aggregator* methods return a single value calculated from all the elements in a sequence. the aggregator methods include `Count`, `Sum`, `Min`, `Max`, `Average`, and `Aggregate`.

## Find the minimum of a sequence of elements

This sample uses `Min` to get the lowest number in an array.

``` cs --region min-syntax --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find the minimum of a projection

This sample uses `Min` to get the length of the shortest word in an array.

``` cs --region min-projection --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find the minimum in each group

This sample uses `Min` to get the cheapest price among each category's products.

``` cs --region min-grouped --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find all elements matching the minimum

This sample uses `Min` to get the products with the cheapest price in each category.

``` cs --region min-each-group --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Aggregates-Max »](./aggregates-3.md) Previous: [Aggregates-Sum »](./aggregates-1.md)**

**[Home](../README.md)**
35 changes: 35 additions & 0 deletions 101-linq-samples/docs/aggregates-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# LINQ - Aggregators

*Aggregator* methods return a single value calculated from all the elements in a sequence. the aggregator methods include `Count`, `Sum`, `Min`, `Max`, `Average`, and `Aggregate`.

## Find the maximun of a sequence of elements

This sample uses `Max` to get the highest number in an array.

``` cs --region max-syntax --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find the maximum of a projection

This sample uses `Max` to get the length of the longest word in an array.

``` cs --region max-projection --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find the maximum in each group

This sample uses `Max` to get the most expensive price among each category's products.

``` cs --region max-grouped --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find all elements matching the maximum

This sample uses `Max` to get the products with the most expensive price in each category.

``` cs --region max-each-group --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Aggregates-Average »](./aggregates-4.md) Previous: [Aggregates-Min »](./aggregates-2.md)**

**[Home](../README.md)**
42 changes: 42 additions & 0 deletions 101-linq-samples/docs/aggregates-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# LINQ - Aggregators

*Aggregator* methods return a single value calculated from all the elements in a sequence. the aggregator methods include `Count`, `Sum`, `Min`, `Max`, `Average`, and `Aggregate`.

## Find the average of a sequence of elements

This sample uses `Average` to get the average of all numbers in an array.

``` cs --region average-syntax --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find the average of a projection

This sample uses `Average` to get the average length of the words in the array.

``` cs --region average-projection --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Find the average in each group

This sample uses `Average` to get the average price of each category's products.

``` cs --region average-grouped --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Compute an aggregate value from all elements of a sequence

This sample uses `Aggregate` to create a running product on the array that calculates the total product of all elements.

``` cs --region aggregate-syntax --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Compute an aggregate value from a seed value and all elements of a sequence

This sample uses `Aggregate` to create a running account balance that subtracts each withdrawal from the initial balance of 100, as long as the balance never drops below 0.

``` cs --region aggregate-seeded --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Sequence operations »](./sequence-operations.md) Previous: [Aggregates-Min »](./aggregates-2.md)**

**[Home](../README.md)**
36 changes: 36 additions & 0 deletions 101-linq-samples/docs/aggregates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# LINQ - Aggregators

*Aggregator* methods return a single value calculated from all the elements in a sequence. the aggregator methods include `Count`, `Sum`, `Min`, `Max`, `Average`, and `Aggregate`.

## Count all elements in a sequence

This sample uses `Count` to get the number of unique factors of 300.

``` cs --region count-syntax --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Count all elements matching a condition

This sample uses `Count` to get the number of odd ints in the array.


``` cs --region count-conditional --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Count all elements nested in a query

This sample uses `Count` to return a list of customers and how many orders each has.

``` cs --region nested-count --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

## Count all elements that are members of a group

This sample uses `Count` to return a list of categories and how many products each has.

``` cs --region grouped-count --source-file ../src/AggregateOperators.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Aggregates-Sum »](./aggregates-1.md) Previous: [Quantifiers »](./generators.md)**

**[Home](../README.md)**
35 changes: 35 additions & 0 deletions 101-linq-samples/docs/conversions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# LINQ - Conversion Operators

The methods `ToArray`, `ToArray`, `ToDictionary`, and `OfType` provide ways to convert LINQ results to collections.

## Convert to an array

This sample uses `ToArray` to immediately evaluate a sequence into an array.

``` cs --region convert-to-array --source-file ../src/Conversions.cs --project ../src/Try101LinqSamples.csproj
```

## Convert to a list

This sample uses `ToList` to immediately evaluate a sequence into a `List<T>`.

``` cs --region convert-to-list --source-file ../src/Conversions.cs --project ../src/Try101LinqSamples.csproj
```

## Convert to a dictionary

This sample uses `ToDictionary` to immediately evaluate a sequence and a related key expression into a dictionary.

``` cs --region convert-to-dictionary --source-file ../src/Conversions.cs --project ../src/Try101LinqSamples.csproj
```

## Convert elements that match a type

This sample uses `OfType` to return only the elements of the array that are of type `double`.

``` cs --region convert-to-type --source-file ../src/Conversions.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Element operations &raquo;](./elements.md) Previous: [Intersect and except set operations &raquo;](./sets-2.md)**

**[Home](../README.md)**
42 changes: 42 additions & 0 deletions 101-linq-samples/docs/elements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# LINQ - Element Operators

The methods `First`, `FirstOrDefault`, and `ElementAt` provide ways to select a single element by position.

## Find the first element

This sample uses `First` to return the first matching element as a `Product`, instead of as a sequence containing a `Product`.

``` cs --region first-element --source-file ../src/ElementOperations.cs --project ../src/Try101LinqSamples.csproj
```

## Find the first matching element

This sample uses `First` to find the first element in the array that starts with 'o'.

``` cs --region first-matching-element --source-file ../src/ElementOperations.cs --project ../src/Try101LinqSamples.csproj
```

## First element of a possibly empty sequence

This sample uses `FirstOrDefault` to try to return the first element of the sequence, unless there are no elements, in which case the default value for that type is returned.

``` cs --region first-or-default --source-file ../src/ElementOperations.cs --project ../src/Try101LinqSamples.csproj
```

## First matching element or default

This sample uses `FirstOrDefault` to return the first product whose `ProductID` is `789` as a single `Product` object, unless there is no match, in which case `null` is returned.

``` cs --region first-matching-or-default --source-file ../src/ElementOperations.cs --project ../src/Try101LinqSamples.csproj
```

## Find element at position

This sample uses `ElementAt` to retrieve the second number greater than 5 from an array.

``` cs --region element-at --source-file ../src/ElementOperations.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Generate sequences &raquo;](./generators.md) Previous: [Conversion operations &raquo;](./conversions.md)**

**[Home](../README.md)**
21 changes: 21 additions & 0 deletions 101-linq-samples/docs/generators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# LINQ - Generate elements

The methods `Range` and `Repeat` create sequences of integers. These can be the source sequences of queries.

## Create a range of numbers

This sample uses `First` to return the first matching element as a `Product`, instead of as a sequence containing a `Product`.

``` cs --region generate-range --source-file ../src/Generators.cs --project ../src/Try101LinqSamples.csproj
```

## Repeat the same number

This sample uses `First` to find the first element in the array that starts with 'o'.

``` cs --region generate-repeat --source-file ../src/Generators.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Quantifying operations &raquo;](./quantifiers.md) Previous: [Conversion operations &raquo;](./conversions.md)**

**[Home](../README.md)**
18 changes: 18 additions & 0 deletions 101-linq-samples/docs/groupings-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# LINQ - Grouping Operators with custom comparer

The `group by` and `into` can use custom comparers to separate groups.

These samples use the following custom comparer. It compares two strings to see if they are anagrams. (Anagrams are pairs of words formed from the same letters.)

``` cs --region anagram-comparer --session groupby-custom-comparer --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

## Groupby with a custom comparer


``` cs --region groupby-custom-comparer --session groupby-custom-comparer --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Nested groupings with a custom comparer &raquo;](./groupings-3.md) Previous: [Groupings &laquo;](./groupings.md)**

**[Home](../README.md)**
18 changes: 18 additions & 0 deletions 101-linq-samples/docs/groupings-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# LINQ - Nested Grouping Operators with custom comparer

The `group by` and `into` can use custom comparers to separate groups. These queries can be nested.

These samples use the following custom comparer. It compares two strings to see if they are anagrams. (Anagrams are pairs of words formed from the same letters.)

``` cs --region anagram-comparer --session nested-groupby-custom --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

## Groupby with a custom comparer


``` cs --region nested-groupby-custom --session nested-groupby-custom --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Set operations &raquo;](./sets.md) Previous: [Groupings with a custom comparer &laquo;](./orderings-2.md)**

**[Home](../README.md)**
35 changes: 35 additions & 0 deletions 101-linq-samples/docs/groupings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# LINQ - Grouping Operators

The `group by` and `into` keywords provide grouping constructs to organize elements of the input sequence into buckets.

## Group by into buckets

This sample demonstrates the use of `group by` and `into` to create buckets based on the remainder of an integer when dividing it by 5.

``` cs --region groupby-syntax --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

## Groupby using a property

This sample uses `group by` to partition a list of words by their first letter.

``` cs --region groupby-property --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

## Grouping using a key property

This sample uses `group by` to partition a list of products by category.

``` cs --region groupby-category --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

## Nested group by queries

This sample demonstrates the use of `group by` and `into` nested buckets of orders by customer, year, and month. The result

``` cs --region nested-groupby --source-file ../src/Groupings.cs --project ../src/Try101LinqSamples.csproj
```

**Next: [Grouping with a custom comparer &raquo;](./groupings-2.md) Previous: [Nested custom comparisons &laquo;](./orderings-5.md)**

**[Home](../README.md)**
35 changes: 35 additions & 0 deletions 101-linq-samples/docs/join-operators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# LINQ - Join operations

Learn to use cross `join`, group `join`, and left outer `join` operations in LINQ queries

## Cross join

This sample shows how to efficiently join elements of two sequences based on equality between key expressions over the two.

``` cs --region cross-join --source-file ../src/JoinOperations.cs --project ../src/Try101LinqSamples.csproj
```

## Group join

Using a group `join` you can get all the products that match a given category bundled as a sequence.

``` cs --region group-join --source-file ../src/JoinOperations.cs --project ../src/Try101LinqSamples.csproj
```

## Cross join with group join

The group `join` operator is more general than `join`, as this slightly more verbose version of the cross `join` sample shows.

``` cs --region cross-group-join --source-file ../src/JoinOperations.cs --project ../src/Try101LinqSamples.csproj
```

## Left outer join using group join

A so-called outer join can be expressed with a `group join`. A left outer join is like a cross join, except that all the left hand side elements get included at least once, even if they don't match any right hand side elements. Note how `Vegetables` shows up in the output even though it has no matching products.

``` cs --region left-outer-join --source-file ../src/JoinOperations.cs --project ../src/Try101LinqSamples.csproj
```

**Previous: [Query execution &raquo;](./query-execution.md)**

**[Home](../README.md)**
Loading