Classification of Standard Query Operators by Manner of Execution

Classification of Standard Query Operators by Manner of Execution

 

The LINQ to Objects implementations of the standard query operator methods execute in one of two main ways: immediate or deferred. The query operators that use deferred execution can be additionally divided into two categories: streaming and non-streaming. If you know how the different query operators execute, it may help you understand the results that you get from a given query. This is especially true if the data source is changing or if you are building a query on top of another query. This topic classifies the standard query operators according to their manner of execution.

Immediate execution means that the data source is read and the operation is performed at the point in the code where the query is declared. All the standard query operators that return a single, non-enumerable result execute immediately.

Deferred execution means that the operation is not performed at the point in the code where the query is declared. The operation is performed only when the query variable is enumerated, for example by using a foreach (For Each in Visual Basic) statement. This means that the results of executing the query depend on the contents of the data source when the query is executed rather than when the query is defined. If the query variable is enumerated multiple times, the results might differ every time. Almost all the standard query operators whose return type is IEnumerable<T> or IOrderedEnumerable<TElement> execute in a deferred manner.

Query operators that use deferred execution can be additionally classified as streaming or non-streaming.

Streaming operators do not have to read all the source data before they yield elements. At the time of execution, a streaming operator performs its operation on each source element as it is read and yields the element if appropriate. A streaming operator continues to read source elements until a result element can be produced. This means that more than one source element might be read to produce one result element.

Non-streaming operators must read all the source data before they can yield a result element. Operations such as sorting or grouping fall into this category. At the time of execution, non-streaming query operators read all the source data, put it into a data structure, perform the operation, and yield the resulting elements.

The following table classifies each standard query operator method according to its method of execution.

System_CAPS_noteNote

If an operator is marked in two columns, two input sequences are involved in the operation, and each sequence is evaluated differently. In these cases, it is always the first sequence in the parameter list that is evaluated in a deferred, streaming manner.

Standard Query Operator

Return Type

Immediate Execution

Deferred Streaming Execution

Deferred Non-Streaming Execution

Aggregate<TSource>

TSource

X

 

 

All<TSource>

Boolean

X

 

 

Any<TSource>

Boolean

X

 

 

AsEnumerable<TSource>

IEnumerable<T>

 

X

 

Average

Single numeric value

X

 

 

Cast<TResult>

IEnumerable<T>

 

X

 

Concat<TSource>

IEnumerable<T>

 

X

 

Contains<TSource>

Boolean

X

 

 

Count<TSource>

Int32

X

 

 

DefaultIfEmpty<TSource>

IEnumerable<T>

 

X

 

Distinct<TSource>

IEnumerable<T>

 

X

 

ElementAt<TSource>

TSource

X

 

 

ElementAtOrDefault<TSource>

TSource

X

 

 

Empty<TResult>

IEnumerable<T>

X

 

 

Except<TSource>

IEnumerable<T>

 

X

 X

First<TSource>

TSource

X

 

 

FirstOrDefault<TSource>

TSource

X

 

 

GroupBy<TSource, TKey>

IEnumerable<T>

 

 

X

GroupJoin

IEnumerable<T>

 

X

Intersect

IEnumerable<T>

 

X

X

Join

IEnumerable<T>

 

X

X

Last<TSource>

TSource

X

 

 

LastOrDefault<TSource>

TSource

X

 

 

LongCount<TSource>

Int64

X

 

 

Max

Single numeric value, TSource, or TResult

X

 

 

Min

Single numeric value, TSource, or TResult

X

 

 

OfType<TResult>

IEnumerable<T>

 

X

 

OrderBy<TSource, TKey>

IOrderedEnumerable<TElement>

 

 

X

OrderByDescending<TSource, TKey>

IOrderedEnumerable<TElement>

 

 

X

Range

IEnumerable<T>

 

X

 

Repeat<TResult>

IEnumerable<T>

 

X

 

Reverse<TSource>

IEnumerable<T>

 

 

X

Select<TSource, TResult>

IEnumerable<T>

 

X

 

SelectMany

IEnumerable<T>

 

X

 

SequenceEqual

Boolean

X

 

 

Single<TSource>

TSource

X

 

 

SingleOrDefault<TSource>

TSource

X

 

 

Skip<TSource>

IEnumerable<T>

 

X

 

SkipWhile<TSource>

IEnumerable<T>

 

X

 

Sum

Single numeric value

X

 

 

Take<TSource>

IEnumerable<T>

 

X

 

TakeWhile<TSource>

IEnumerable<T>

 

X

 

ThenBy<TSource, TKey>

IOrderedEnumerable<TElement>

 

 

X

ThenByDescending<TSource, TKey>

IOrderedEnumerable<TElement>

 

 

X

ToArray<TSource>

TSource array

X

 

 

ToDictionary<TSource, TKey>

Dictionary<TKey, TValue>

X

 

 

ToList<TSource>

IList<T>

X

 

 

ToLookup<TSource, TKey>

ILookup<TKey, TElement>

X

 

 

Union<TSource>

IEnumerable<T>

 

X

 

Where<TSource>

IEnumerable<T>

 

X

 

Show:
© 2016 Microsoft