Appendix. The standard query operators

Filtering

Operator name

Description

OfType Selects values, depending on their ability to be cast to a specified type.
Where Selects values, depending on a predicate function.

Projection

Operator name

Description

Select Selects values, depending on a selector function.
SelectMany Selects values, depending on a selector function, and combines resulting sequences into one sequence. SelectMany performs a one-to-many element projection over a sequence. It differs from Select in that the selector function is expected to return a sequence that is then expanded.

Partitioning

Operator name

Description

Skip Skips n elements from a sequence.
SkipWhile Skips elements based on a predicate function until an element does not satisfy the condition.
Take Takes n elements from a sequence.
TakeWhile Takes elements based on a predicate function until an element does not satisfy the condition.

Join

Operator name

Description

GroupJoin Joins two sequences based on key selector functions and groups the resulting matches for each element.
Join Joins two sequences based on key selector functions and extracts pairs of values.

Concatenation

Operator name

Description

Concat Concatenates two sequences to form one sequence.

Sorting

Operator name

Description

OrderBy Sorts values in ascending order.
OrderByDescending Sorts values in descending order.
ThenBy Performs a secondary sort in ascending order.
ThenByDescending Performs a secondary sort in descending order.
Reverse Reverses the order of the elements in a sequence.

Grouping

Operator name

Description

GroupBy Groups elements that share a common attribute. Each group is represented by an IGrouping<TKey, TElement> object.
ToLookup Inserts elements into a Lookup<TKey, TElement> (a one-to-many dictionary) based on a key selector function.

Set

Operator name

Description

Distinct Removes duplicate values from a collection.
Except Returns the set difference, which means the elements of one sequence that do not appear in a second sequence.
Intersect Returns the set intersection, which means elements that appear in each of two sequences.
Union Returns the set union, which means unique elements that appear in either of two sequences.

Conversion

Operator name

Description

AsEnumerable Returns the input typed as IEnumerable<T>.
AsQueryable Converts a (generic) IEnumerable to a (generic) IQueryable.
Cast Casts the elements of a sequence to a specified type.
OfType Selects values, depending on their ability to be cast to a specified type.
ToArray Converts a collection to an array. This method forces query execution.
ToDictionary Puts elements into a (one-to-one) Dictionary<TKey, TValue> based on a key selector function.
ToList Converts a collection to a List<T>.
ToLookup Inserts elements into a Lookup<TKey, TElement> (a one-to-many dictionary) based on a key selector function.

 

Note

By convention, the “ToXXX” operators cause the queries to execute. The “AsXXX” operators do not. This applies to the conversion operators, but should be respected for clarity for other operators as well, including custom ones.

 

Equality

Operator name

Description

SequenceEqual Determines whether two sequences are equal by comparing elements in a pair-wise manner.

Element

Operator name

Description

ElementAt Returns the element at a specified index in a sequence.
ElementAtOrDefault Returns the element at a specified index in a sequence or default(T) if the index is out of range.
First Returns the first element of a sequence, or the first element that satisfies a condition.
FirstOrDefault Returns the first element of a sequence, or the first element that satisfies a condition. Returns default(T) if no such element exists.
Last Returns the last element of a sequence, or the last element that satisfies a condition.
LastOrDefault Returns the last element of a sequence, or the last element that satisfies a condition. Returns default(T) if no such element exists.
Single Returns the only element of a sequence, or the only element that satisfies a condition. Raises an InvalidOperationException if the sequence does not contain exactly one element.
SingleOrDefault Returns the only element of a sequence, or the only element that satisfies a condition. Returns default(T) if no such element exists. Raises an InvalidOperationException if the sequence contains more than one element.

Generation

Operator name

Description

DefaultIfEmpty Replaces an empty sequence with a default valued singleton sequence.
Empty Returns an empty sequence.
Range Generates a sequence of integral numbers within a specified range.
Repeat Generates a sequence that contains one repeated value.

Quantifiers

Operator name

Description

All Determines whether all the elements in a sequence satisfy a condition.
Any Determines whether any elements in a sequence satisfy a condition.
Contains Determines whether a sequence contains a specified element.

Aggregation

Operator name

Description

Aggregate Performs a custom aggregation operation on the values of a sequence.
Average Calculates the average value of a sequence of values.
Count Counts the elements in a sequence, optionally only those elements that satisfy a predicate function.
LongCount Counts the elements in a large sequence, optionally only those elements that satisfy a predicate function.
Max Determines the maximum value in a sequence.
Min Determines the minimum value in a sequence.
Sum Calculates the sum of the values in a sequence.

 

Note

In general, operators that return something other than an IEnumerable<T> will cause immediate query execution.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10