LINQ Standard Query Operators
Standard Query Operators are the “heart” of LINQ, as those operators are representing the API that we need to go through in order to do any LINQ query.
There are two sets of LINQ standard query operators, one that operates on objects of type
IEnumerable and the other that operates on objects of type
They are defined as extension methods of the type that they operate on. This means that they can be called by using either static method syntax or instance method syntax.
The Standard Query Operators operate on sequences. Any object that implements the interface
IEnumerable for some type
T is considered a sequence of that type.
Thanks to the fact that query operators are mainly extension methods working with
IQueriable objects, you can easily create your own ones.
Standard Query Operator groups
The table below classifies the standard query operators according to the type of
operation they perform.
|Partitioning||Skip, SkipWhile, Take, TakeWhile|
|Ordering||OrderBy, OrderByDescending, ThenBy, ThenByDescending|
|Set||Distinct, Except, Intersect, Union|
|Conversion||AsEnumerable, AsQueryable, Cast, ToArray, ToDictionary, ToList|
|Element||ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single,|
|Generation||DefaultEmpty, Empty, Range, Repeat|
|Quantifiers||All, Any, Contains|
|Aggregation||Aggregate, Average, Count, LongCount, Max, Min, Sum|
Filtering refers to the operation of restricting the result set to contain only those elements that satisfy a specified condition.
OfType: Selects values, depending on their ability to be cast to a specified type.
Where: Selects values that are based on a predicate function.
Projection refers to the operation of transforming an object into a new form. By using projection, you can build a new type that can be built or already defined in the query (original object). You can project a property and perform a mathematical function on it.
Select: Projects values that are based on a transform function.
SelectMany: Projects sequences of values that are based on a transform function and then flattens them into one sequence.
Partitioning in LINQ refers to the operation of dividing an input sequence into two sections, without rearranging the elements, and then returning one of the sections.
Skip: Skips elements up to a specified position in a sequence.
SkipWhile: Skips elements based on a predicate function until an element does not satisfy the condition.
Take: Takes elements up to a specified position in a sequence.
TakeWhile: Takes elements based on a predicate function until an element does not satisfy the condition.
A join of two data sources is the association of objects in one data source with objects that share a common attribute in another data source.
Join: Joins two sequences based on key selector functions and extracts pairs of values.
GroupJoin: Joins two sequences based on key selector functions and groups the resulting matches for each element.
Concatenation refers to the operation of appending one sequence to another.
Concat: Concatenates two sequences to form one sequence.
An ordering operation orders the elements of a sequence based on one or more attributes. The first sort criterion performs a primary sort on the elements.
By specifying a second sort criterion, you can sort the elements within each primary sort group.
OrderBy: Sorts values in ascending order.
OrderByDescending: Sorts values in a descending order.
Reverse: Reverses the order of the elements in a collection.
ThenBy: Performs a secondary sort in ascending order.
ThenByDescending: Performs a secondary sort in a descending order.
Grouping refers to the operation of putting data into groups so that the elements in each group share a common attribute.
GroupBy: Groups elements that share a common attribute.
ToLookup: Inserts elements into a Lookup (a one-to-many dictionary) based on a key selector function.
Set operations refer to query operations that produce a result set based on the presence or absence of equivalent elements within the same or separate collections (or sets).
Distinct: Removes duplicate values from a collection
Except: Returns the set difference, which means the elements of one
collection that do not appear in a second collection.
Intersect: Returns the set intersection, which means elements that
appear in each of two collections.
Union: Returns the set union, which means unique elements that appear in either
of two collections.
Conversion methods change the type of input objects.
AsEnumerable: Returns the input typed as IEnumerable.
AsQueryable: Converts a (generic) IEnumerable to a (generic) IQueryable.
Cast: Casts the elements of a collection to a specified type.
OfType: Filters 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 Dictionarybased on a key selector function. This method forces query execution.
ToList: Converts a collection to a List. This method forces query execution.
ToLookup: Puts elements into a Lookup(a one-to-many dictionary) based on a key selector function. This method forces query execution.
Two sequences whose corresponding elements are equal and which have the same number of elements are considered equal.
SequenceEqual:Determines whether two sequences are equal by comparing elements in a pair-wise manner.
Element operations return a single, specific element from a sequence.
ElementAt: Returns the element at a specified index in a collection.
ElementAtOrDefault: Returns the element at a specified index in a collection or a default value if the index is out of range.
First: Returns the first element of a collection, or the first element that satisfies a condition.
FirstOrDefault: Returns the first element of a collection, or the first element that satisfies a condition. Returns a default value if no such element exists.
Last: Returns the last element of a collection, or the last element that satisfies a condition.
LastOrDefault: Returns the last element of a collection, or the last element that satisfies a condition. Returns a default value if no such element exists.
Single: Returns the only element of a collection, or the only element that satisfies a condition.
SingleOrDefault: Returns the only element of a collection, or the only element that satisfies a condition (by michelle at dh fashion). Returns a default value if no such element exists or the collection does not contain exactly one element.
Generation refers to creating a new sequence of values.
DefaultIfEmpty: Replaces an empty collection with a default valued singleton collection.
Empty: Returns an empty collection.
Range: Generates a collection that contains a sequence of numbers.
Repeat: Generates a collection that contains one repeated value.
Quantifier operations return a boolean value that indicates whether some or all of the elements in a sequence satisfy a condition.
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.
An aggregation operation computes a single value from a collection of values.
Aggregate: Performs a custom aggregation operation on the values of a collection.
Average: Calculates the average value of a collection of values.
Count: Counts the elements in a collection, optionally only those elements that satisfy a predicate function.
LongCount: Counts the elements in a large collection, optionally only those elements that satisfy a predicate function.
Max: Determines the maximum value in a collection.
Min: Determines the minimum value in a collection.
Sum: Calculates the sum of the values in a collection.
- Standard Query Operators Overview