|
| 1 | +# 101 LINQ Samples |
| 2 | + |
| 3 | + |
| 4 | +<p align ="center"> |
| 5 | +<img src ="https://user-images.githubusercontent.com/2546640/56708992-deee8780-66ec-11e9-9991-eb85abb1d10a.png" width="350"> |
| 6 | +</p> |
| 7 | + |
| 8 | +## Exploring LINQ using this tutorial |
| 9 | + |
| 10 | +This tutorial starts with the [fundamentals of LINQ](docs/restrictions.md). You can follow each page in order to explore all the elements of LINQ. Following step-by-step lets you explore LINQ from these fundamental queries through more complicated queries, up to the most complex uses. |
| 11 | + |
| 12 | +Alternatively, if you're familiar with LINQ, you can jump to a specific section to refresh your knowledge, or learn new techniques. Here is the full list of samples: |
| 13 | + |
| 14 | +### [Restriction operators](docs/restrictions.md): The `where` keyword |
| 15 | + |
| 16 | +The `where` keyword or `Where` method provide this capability. These operators restrict, or filter, the input sequence to produce an output sequence. |
| 17 | + |
| 18 | +- [Your first query](docs/restrictions.md#linq-query-structure): the structure of a LINQ query. |
| 19 | +- [Filter elements on a property](docs/restrictions.md#filter-elements-on-a-property): filter elements based on a single property |
| 20 | +- [Filter elements using multiple properties](docs/restrictions.md#filter-elements-on-multiple-properties): test multiple properties to filter elements in a sequence. |
| 21 | +- [Filter and drilldown into the output elements](docs/restrictions.md#examine-a-sequence-property-of-output-elements): filter input elements, then drill into a sequence on each output element. |
| 22 | +- [Filter input elements based on index](docs/restrictions.md#filter-elements-based-on-position): use an element's position to filter it. |
| 23 | + |
| 24 | +### [Projection operators](docs/projections.md): The `select` keyword |
| 25 | + |
| 26 | +The `select` keyword or `Select` method provide this capability. These operators create output sequence elements from input sequence elements. The output elements may be either the same or different types. |
| 27 | + |
| 28 | +- [Select clause](docs/projections.md#select-clause): Select clause structure |
| 29 | +- [Select one property the input elements](docs/projections.md#select-a-single-property): Select a single property from an input element. |
| 30 | +- [Transform to an element of another sequence](docs/projections.md#transform-with-select): Map an input sequence element to an output sequence element. |
| 31 | +- [Select anonymous types or tuples](docs/projections-2.md#select-anonymous-types-or-tuples): Select output elements that are anonymous types or tuples. |
| 32 | +- [Create new types with select](docs/projections-2.md#use-select-to-create-new-types): Construct new properties for output elements. |
| 33 | +- [Select a subset of properties into a new type](docs/projections-2.md#select-a-subset-of-properties): Trip the data selected for output elements. |
| 34 | +- [Select the index of source item](docs/projections-3.md#select-with-index-of-item): Include an elements position as part of the projection. |
| 35 | +- [Combine select and where](docs/projections-3.md#select-combined-with-where): Filter output elements before selecting properties. |
| 36 | +- [Combine multiple input sequences](docs/projections-4.md#select-from-multiple-input-sequences): Combine every element of the first sequence with each element of a second input sequence. |
| 37 | +- [Select from related input sequences](docs/projections-4.md#select-from-related-input-sequences): Filter on a property of a nested sequence. |
| 38 | +- [Select multiple sequences with filters](docs/projections-4.md#compound-select-with-where-clause): Filter on properties of several related input sequences. |
| 39 | +- [Select from related sequences ](docs/projections-5.md#compound-select-with-where-and-assignment): Cache a nested sequence to select properties from it. |
| 40 | +- [Select many with multiple filters](docs/projections-5.md#compound-select-with-multiple-where-clauses): Filter elements on multiple input sequences. |
| 41 | +- [Select many with element index](docs/projections-5.md#compound-select-with-index): Select the index of an input element along with other properties of the element. |
| 42 | + |
| 43 | +## [Partition operators](docs/partitions.md) |
| 44 | + |
| 45 | +Use the `Take`, `Skip`, `TakeWhile` and `SkipWhile` methods to partition the input sequence. You can get a slice of the input sequence as the output sequence. |
| 46 | + |
| 47 | +- [Take the first elements](docs/partitions.md#take-elements): Take no more than a number of elements. |
| 48 | +- [Take from nested results](docs/partitions.md#nested-take-partitions): Query nested sources and take a set of results. |
| 49 | +- [Skip the first elements](docs/partitions.md#skip-elements): Start enumerating after a set of elements. |
| 50 | +- [Skip from nested results](docs/partitions.md#nested-skip-partitions): Query nested sources and skip the first results |
| 51 | +- [Take based on a condition](docs/partitions-2.md#takewhile-syntax): Take elements while a condition is true.. |
| 52 | +- [Indexed TakeWhile method](docs/partitions-2.md#indexed-takewhile): Take based on a condition and the index of an element. |
| 53 | +- [Skip based on a condition](docs/partitions-2.md#skipwhile-syntax): Skip elements as long as a condition is true. |
| 54 | +- [Indexed SkipWhile method](docs/partitions-2.md#indexed-skipwhile): Skip based on a condition and the index of an element. |
| 55 | + |
| 56 | +## [Ordering operators](docs/orderings.md) |
| 57 | + |
| 58 | +The `orderby` keyword, along with `descending`, and the `OrderBy`, `ThenBy`, `OrderbyDescending` and `ThenByDescending` LINQ queries are used to sort data output. |
| 59 | + |
| 60 | +- [Sort elements](docs/orderings.md#orderby-sorts-elements): Sort elements using the standard sort order. |
| 61 | +- [Orderby using a single property](docs/orderings.md#orderby-property): You can sort elements on a single property value |
| 62 | +- [Order types you've defined](docs/orderings.md#ordering-user-defined-types): You can use any property that has an ordering relation to order types you've created. |
| 63 | +- [Sort in descending order](docs/orderings-2.md#orderby-descending): Add `descending` to sort in descending order |
| 64 | +- [Orderby descending on types you define](docs/orderings-2.md#descending-ordering-user-defined-types): With a defined sort order, you can sort in descending order. |
| 65 | +- [Thenby to define a secondary sort](docs/orderings-3.md#orderby-multiple-properties): You can define secondary sorts when the primary sort keys are equal. |
| 66 | +- [Thenby descending](docs/orderings-3.md#multiple-ordering-descending): Try multiple sort orders in descending order. |
| 67 | +- [Reverse the input sequence](docs/orderings-3.md#reverse-the-sequence): The `Reverse` method reverses an input sequence. |
| 68 | +- [Orderby with a custom compare function](docs/orderings-4.md#ordering-with-a-custom-comparer): You can define a custom compare function to sort elements. |
| 69 | +- [Orderby descending with a custom compare function](docs/orderings-4.md#descending-orders-with-a-custom-comparer): Descending sort can use a custom comparer. |
| 70 | +- [Thenby using a comparer](docs/orderings-5.md#multiple-ordering-with-a-custom-comparer): You can mix a standard sort order with a custom comparer. |
| 71 | +- [Thenby descending using a comparer](docs/orderings-5.md#multiple-descending-order-with-a-custom-comparer): You can also sort in descending order. |
| 72 | + |
| 73 | +## [Grouping operators](docs/groupings.md) |
| 74 | + |
| 75 | +The `GroupBy` and `into` operators organize a sequence into buckets. |
| 76 | + |
| 77 | +- [Group by into buckets](docs/groupings.md#group-by-into-buckets): Group items into buckets based on a property. |
| 78 | +- [Group by using a property](docs/groupings.md#groupby-using-a-property): Group items based on a property of the input sequence. |
| 79 | +- [Group by using a key property](docs/groupings.md#grouping-using-a-key-property): Group items based on a key value. |
| 80 | +- [Nested group by queries](docs/groupings.md#nested-group-by-queries): Nest groups of buckets into a tree structure. |
| 81 | +- [Group by using a custom comparer](docs/groupings-2.md): Group items based on a custom function. |
| 82 | +- [Nested group by using a custom comparer](docs/groupings-3.md): Build a nested tree structure based on a custom comparer. |
| 83 | + |
| 84 | +## [Set Operators](docs/sets.md) |
| 85 | + |
| 86 | +These operators provide functionality to compare multiple sets of data. You can find the intersection, union, all distinct elements and the difference between sets. |
| 87 | + |
| 88 | +- [Find distinct elements](docs/sets.md#find-distinct-elements): Build a set of the distinct elements in two sets. |
| 89 | +- [Find all distinct values of a single property](docs/sets.md#find-distinct-values-of-a-property): Build a combined set based on values of a single property. |
| 90 | +- [Find the union of sets](docs/sets.md#find-the-union-of-sets): Build a set that is the union of two sets. |
| 91 | +- [Union of query results](docs/sets.md#union-of-query-results): Find the union of two query results. |
| 92 | +- [Find the intersection of sets](docs/sets-2.md#find-the-intersection-of-two-sets): Produce a set that is the intersection of two sets. |
| 93 | +- [Find the intersection of query results](docs/sets-2.md#find-the-intersection-of-query-results): Build the intersection of two query result sets. |
| 94 | +- [Find the difference of two sets using except](docs/sets-2.md#find-the-difference-of-two-sets): Find elements in the first set that aren't present in the section set. |
| 95 | +- [Find the difference of two queries using except](docs/sets-2.md#difference-of-two-queries): Find elements in the first result set that are not present in the second result set. |
| 96 | + |
| 97 | +## [Conversion operators](docs/conversions.md) |
| 98 | + |
| 99 | +Sometimes you want to convert a query result set to a different kind of collection. These operators show how you can do this. |
| 100 | + |
| 101 | +- [Convert results to an Array](docs/conversions.md#convert-to-array): Convert an output sequence to an `Array`. |
| 102 | +- [Convert results to a list](docs/conversions.md#convert-to-list): Convert an output sequence to a `List<T>`. |
| 103 | +- [Convert results to a dictionary](docs/conversions.md#convert-to-dictionary): Convert the results of a query to a `Dictionary<TKey, TValue>` based on a property of the output sequence elements. |
| 104 | +- [Convert results based on item type](docs/conversions.md#convert-elements-that-match-a-type): Produce an output sequence of only those input elements that match a given type. |
| 105 | + |
| 106 | +## [Element operators](docs/elements.md) |
| 107 | + |
| 108 | +The methods `First`, `FirstOrDefault`, `Last`, `LastOrDefault`, and `ElementAt` retrieve elements based on the position of that element in the sequence. |
| 109 | + |
| 110 | +- [Retrieve the first element](docs/elements.md#find-the-first-element): This examples assumes the sequence has at least one element. |
| 111 | +- [Retrieve the first matching element](docs/elements.md#find-the-first-matching-element): Find the first element that matches a condition. At least one matching element must exist. |
| 112 | +- [Retrieve the first element or default](docs/elements.md#first-element-of-a-possibly-empty-sequence): Find the first element, or `null`, if the sequence is empty. |
| 113 | +- [Retrieve the first matching element or default](docs/elements.md#first-matching-element-or-default): Retrieve the first matching element, or null. |
| 114 | +- [Find an element by position](docs/elements.md#find-element-at-position): Retrieve the element at a certain position. |
| 115 | + |
| 116 | +## [Generate sequences](docs/generators.md) |
| 117 | + |
| 118 | +These methods generate sequences of integers. |
| 119 | + |
| 120 | +- [Generate a range of numbers](docs/generators.md#create-a-range-of-numbers): Generate sequential numbers. |
| 121 | +- [Repeat the same number](docs/generators.md#repeat-the-same-number): Generate the samve value for each element. |
| 122 | + |
| 123 | +## [Quantifying members](docs/quantifiers.md) |
| 124 | + |
| 125 | +The methods `Any` and `All` test for elements that match a condition. `Any` checks for one element. `All` checks that all elements match the condition. |
| 126 | + |
| 127 | +- [Check if any elements match a condition](docs/quantifiers.md#check-for-any-matching-elements): Does at least one element match a condition? |
| 128 | +- [Group elements that match a condition](docs/quantifiers.md#group-by-any-elements-matching-a-condition): Group elements by those that match a conition. |
| 129 | +- [Check if all elements match a condition](docs/quantifiers.md#check-that-all-matching-elements): Do all elements match a condition? |
| 130 | +- [Group where all elements match a condition](docs/quantifiers.md#group-by-all-elements-matching-a-condition): Group all elements that match a given condition. |
| 131 | + |
| 132 | +## [Aggregator operators](docs/aggregates.md) |
| 133 | + |
| 134 | +There are a number of methods that perform calculations on values in a sequence. Note that some of these methods require that the input sequence is a numeric type. Those methods are noted below. |
| 135 | + |
| 136 | +- [Count all elements in a sequence](docs/aggregates.md#count-all-elements-in-a-sequence): Count all elements works on any type. |
| 137 | +- [Count all elements matching a condition in a sequence](docs/aggregates.md#count-all-elements-matching-a-condition): Count all elements that match a conditin. |
| 138 | +- [Count all elements from a nested query](docs/aggregates.md#count-all-elements-nested-in-a-query): Count all elements in a nested query result. |
| 139 | +- [Count all elements in a group](docs/aggregates.md#count-all-elements-that-are-members-of-a-group): Count all the elements in a group. |
| 140 | +- [Sum all elements in a sequence](docs/aggregates-1.md#sum-all-numeric-elements-in-a-sequence): Sum all elements in a sequence of numbers. |
| 141 | +- [Sum a projection from sequence](docs/aggregates-1.md#sum-a-projection-from-a-sequence): Sum a numeric projection from a sequence of elements. |
| 142 | +- [Sum all elements in a group](docs/aggregates-1.md#sum-all-elements-that-are-members-of-a-group): Sum the numeric elements in a group projection. |
| 143 | +- [Find the minimum element in a sequence](docs/aggregates-2.md#find-the-minimum-of-a-sequence-of-elements): Find the minimum value in a numeric sequence. |
| 144 | +- [Find the minimum of a projection from a sequence](docs/aggregates-2.md#find-the-minimum-of-a-projection): Find the minimum value of a numeric projection from a sequence. |
| 145 | +- [Find the minimum in each group](docs/aggregates-2.md#find-the-minimum-in-each-group): Find the smallest numeric value in a group projection. |
| 146 | +- [Find all minimums in a group](docs/aggregates-2.md#find-all-elements-matching-the-minimum): Find all elements that are the minimum numeric value. |
| 147 | +- [Find the maximum element in a sequence](docs/aggregates-3.md#find-the-maximum-of-a-sequence-of-elements): Find the maximum of a numeric sequence. |
| 148 | +- [Find the maximum of a projection from a sequence](docs/aggregates-3.md#find-the-maximum-of-a-projection): Find the maximum of a numeric projection of a sequence. |
| 149 | +- [Find the maximum in each group](docs/aggregates-3.md#find-the-maximum-in-each-group): Find the maximum numeric value in each output group of a query. |
| 150 | +- [Find all maximums in a group](docs/aggregates-3.md#find-all-elements-matching-the-maximum): Find all numeric values that match the maximum value. |
| 151 | +- [Find the average of all element in a sequence](docs/aggregates-3.md#find-the-average-of-a-sequence-of-elements): Find the average of all numeric values in a sequence. |
| 152 | +- [Find the maximum of a projection from a sequence](docs/aggregates-3.md#find-the-average-of-a-projection): Find the maximum of a numeric projection from a sequence. |
| 153 | +- [Find the maximum in each group](docs/aggregates-3.md#find-the-average-in-each-group): Find the numeric maximum in each group projection. |
| 154 | +- [Compute an aggregate from all elements in a sequence](docs/aggregates-4.md#compute-an-aggregate-value-from-all-elements-of-a-sequence): Compute a single value from all elements in a sequence. These need not be numeric sequences. |
| 155 | +- [Compute an aggregate from a seed and elements in a sequence](docs/aggregates-4.md#compute-an-aggregate-value-from-a-seed-value-and-all-elements-of-a-sequence): Compute a single value from a seed value and a sequence. The elements need not be numeric. |
| 156 | + |
| 157 | +## [Sequence operations](docs/sequence-operations.md) |
| 158 | + |
| 159 | +These methods operate on entire sequences rather than elements of a sequence. They compare sequences or create new sequences from combining all elements. |
| 160 | + |
| 161 | +- [Compare two sequences for equality](docs/sequence-operations.md#count-all-elements-in-a-sequence): Do two sequences contain the same elements in the same order? |
| 162 | +- [Concatenate two sequences](docs/sequence-operations.md#concatenate-projections-from-two-sequences): Create a new sequence from all elements in two source sequences. |
| 163 | +- [Concatenate projections from two sequences](docs/sequence-operations.md#concatenate-projections-from-two-sequences): Create a new sequence from a projection of all elements in two source collections. |
| 164 | +- [Combine sequences with zip](docs/sequence-operations.md#combine-sequences-with-zip): Combine two sequences by producing a new elements from pairs of elements in two source sequences. |
| 165 | + |
| 166 | +## [Eager and lazy query execution](docs/query-execution.md) |
| 167 | + |
| 168 | +You can use different query operations to specify eager execution instead of lazy execution. |
| 169 | + |
| 170 | +- [Queries execute lazily](docs/query-execution.md#queries-execute-lazily): This demonstrates the default query execution. |
| 171 | +- [Request eager execution](docs/query-execution.md#request-eager-query-execution): This forces eager execution to demonstrate teh difference. |
| 172 | +- [Reuse queries with new results](docs/query-execution.md#reuse-queries-with-new-results): Queries executing lazily can produce different results if the source collection changes. |
| 173 | + |
| 174 | +## [Join operations](docs/join-operators.md) |
| 175 | + |
| 176 | +These operations perform similar functions to SQL join operators. These work with any LINQ data source. |
| 177 | + |
| 178 | +- [Cross join](docs/join-operators.md#cross-join): Perform a cross join. |
| 179 | +- [Group join](docs/join-operators.md#group-join): Perform a group join. |
| 180 | +- [Cross join with group join](docs/join-operators.md#cross-join-with-group-join): Perform a cross join combined with a group join. |
| 181 | +- [Left outer join](docs/join-operators.md#left-outer-join-using-group-join): Simulate a left outer join using a group join. |
0 commit comments