101 LINQ Samples
Learn how to use LINQ in your applications with these code samples, covering the entire range of LINQ functionality, demonstrating LINQ with objects. Once you've learned LINQ using object sequences, you can use LINQ with any supported data source.
Exploring LINQ using this tutorial
This tutorial starts with the fundamentals of LINQ. 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.
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:
Restriction operators: The
where keyword or
Where method provide this capability. These operators restrict, or filter, the input sequence to produce an output sequence.
- Your first query: the structure of a LINQ query.
- Filter elements on a property: filter elements based on a single property
- Filter elements using multiple properties: test multiple properties to filter elements in a sequence.
- Filter and drilldown into the output elements: filter input elements, then drill into a sequence on each output element.
- Filter input elements based on index: use an element's position to filter it.
Projection operators: 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.
- Select clause: Select clause structure
- Select one property the input elements: Select a single property from an input element.
- Transform to an element of another sequence: Map an input sequence element to an output sequence element.
- Select anonymous types or tuples: Select output elements that are anonymous types or tuples.
- Create new types with select: Construct new properties for output elements.
- Select a subset of properties into a new type: Trip the data selected for output elements.
- Select the index of source item: Include an elements position as part of the projection.
- Combine select and where: Filter output elements before selecting properties.
- Combine multiple input sequences: Combine every element of the first sequence with each element of a second input sequence.
- Select from related input sequences: Filter on a property of a nested sequence.
- Select multiple sequences with filters: Filter on properties of several related input sequences.
- Select from related sequences : Cache a nested sequence to select properties from it.
- Select many with multiple filters: Filter elements on multiple input sequences.
- Select many with element index: Select the index of an input element along with other properties of the element.
SkipWhile methods to partition the input sequence. You can get a slice of the input sequence as the output sequence.
- Take the first elements: Take no more than a number of elements.
- Take from nested results: Query nested sources and take a set of results.
- Skip the first elements: Start enumerating after a set of elements.
- Skip from nested results: Query nested sources and skip the first results
- Take based on a condition: Take elements while a condition is true..
- Indexed TakeWhile method: Take based on a condition and the index of an element.
- Skip based on a condition: Skip elements as long as a condition is true.
- Indexed SkipWhile method: Skip based on a condition and the index of an element.
orderby keyword, along with
descending, and the
ThenByDescending LINQ queries are used to sort data output.
- Sort elements: Sort elements using the standard sort order.
- Orderby using a single property: You can sort elements on a single property value
- Order types you've defined: You can use any property that has an ordering relation to order types you've created.
- Sort in descending order: Add
descendingto sort in descending order
- Orderby descending on types you define: With a defined sort order, you can sort in descending order.
- Thenby to define a secondary sort: You can define secondary sorts when the primary sort keys are equal.
- Thenby descending: Try multiple sort orders in descending order.
- Reverse the input sequence: The
Reversemethod reverses an input sequence.
- Orderby with a custom compare function: You can define a custom compare function to sort elements.
- Orderby descending with a custom compare function: Descending sort can use a custom comparer.
- Thenby using a comparer: You can mix a standard sort order with a custom comparer.
- Thenby descending using a comparer: You can also sort in descending order.
into operators organize a sequence into buckets.
- Group by into buckets: Group items into buckets based on a property.
- Group by using a property: Group items based on a property of the input sequence.
- Group by using a key property: Group items based on a key value.
- Nested group by queries: Nest groups of buckets into a tree structure.
- Group by using a custom comparer: Group items based on a custom function.
- Nested group by using a custom comparer: Build a nested tree structure based on a custom comparer.
These operators provide functionality to compare multiple sets of data. You can find the intersection, union, all distinct elements and the difference between sets.
- Find distinct elements: Build a set of the distinct elements in two sets.
- Find all distinct values of a single property: Build a combined set based on values of a single property.
- Find the union of sets: Build a set that is the union of two sets.
- Union of query results: Find the union of two query results.
- Find the intersection of sets: Produce a set that is the intersection of two sets.
- Find the intersection of query results: Build the intersection of two query result sets.
- Find the difference of two sets using except: Find elements in the first set that aren't present in the section set.
- Find the difference of two queries using except: Find elements in the first result set that are not present in the second result set.
Sometimes you want to convert a query result set to a different kind of collection. These operators show how you can do this.
- Convert results to an Array: Convert an output sequence to an
- Convert results to a list: Convert an output sequence to a
- Convert results to a dictionary: Convert the results of a query to a
Dictionary<TKey, TValue>based on a property of the output sequence elements.
- Convert results based on item type: Produce an output sequence of only those input elements that match a given type.
ElementAt retrieve elements based on the position of that element in the sequence.
- Retrieve the first element: This examples assumes the sequence has at least one element.
- Retrieve the first matching element: Find the first element that matches a condition. At least one matching element must exist.
- Retrieve the first element or default: Find the first element, or
null, if the sequence is empty.
- Retrieve the first matching element or default: Retrieve the first matching element, or null.
- Find an element by position: Retrieve the element at a certain position.
These methods generate sequences of integers.
- Generate a range of numbers: Generate sequential numbers.
- Repeat the same number: Generate the samve value for each element.
All test for elements that match a condition.
Any checks for one element.
All checks that all elements match the condition.
- Check if any elements match a condition: Does at least one element match a condition?
- Group elements that match a condition: Group elements by those that match a conition.
- Check if all elements match a condition: Do all elements match a condition?
- Group where all elements match a condition: Group all elements that match a given condition.
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.
- Count all elements in a sequence: Count all elements works on any type.
- Count all elements matching a condition in a sequence: Count all elements that match a conditin.
- Count all elements from a nested query: Count all elements in a nested query result.
- Count all elements in a group: Count all the elements in a group.
- Sum all elements in a sequence: Sum all elements in a sequence of numbers.
- Sum a projection from sequence: Sum a numeric projection from a sequence of elements.
- Sum all elements in a group: Sum the numeric elements in a group projection.
- Find the minimum element in a sequence: Find the minimum value in a numeric sequence.
- Find the minimum of a projection from a sequence: Find the minimum value of a numeric projection from a sequence.
- Find the minimum in each group: Find the smallest numeric value in a group projection.
- Find all minimums in a group: Find all elements that are the minimum numeric value.
- Find the maximum element in a sequence: Find the maximum of a numeric sequence.
- Find the maximum of a projection from a sequence: Find the maximum of a numeric projection of a sequence.
- Find the maximum in each group: Find the maximum numeric value in each output group of a query.
- Find all maximums in a group: Find all numeric values that match the maximum value.
- Find the average of all element in a sequence: Find the average of all numeric values in a sequence.
- Find the maximum of a projection from a sequence: Find the maximum of a numeric projection from a sequence.
- Find the maximum in each group: Find the numeric maximum in each group projection.
- Compute an aggregate from all elements in a sequence: Compute a single value from all elements in a sequence. These need not be numeric sequences.
- Compute an aggregate from a seed and elements in a sequence: Compute a single value from a seed value and a sequence. The elements need not be numeric.
These methods operate on entire sequences rather than elements of a sequence. They compare sequences or create new sequences from combining all elements.
- Compare two sequences for equality: Do two sequences contain the same elements in the same order?
- Concatenate two sequences: Create a new sequence from all elements in two source sequences.
- Concatenate projections from two sequences: Create a new sequence from a projection of all elements in two source collections.
- Combine sequences with zip: Combine two sequences by producing a new elements from pairs of elements in two source sequences.
You can use different query operations to specify eager execution instead of lazy execution.
- Queries execute lazily: This demonstrates the default query execution.
- Request eager execution: This forces eager execution to demonstrate teh difference.
- Reuse queries with new results: Queries executing lazily can produce different results if the source collection changes.
These operations perform similar functions to SQL join operators. These work with any LINQ data source.