Standard Query Operators in LINQ
The standard query operators are the methods
that form the Language-Integrated Query (LINQ) pattern. Most of these
methods operate on sequences, where a sequence is an object whose type
implements the IEnumerable<(Of <(T>)>) interface or the IQueryable<(Of <(T>)>) interface.
The standard query operators provide query capabilities
including filtering, projection, aggregation, sorting and more.
List of Standard Query Operators in LINQ
- Aggregation: Average, Count, LongCount, Max, Min, Sum
- Concatenation : Concat
- Conversion: Cast, OfType, ToArray, ToDictionary, ToList, ToLookup, ToSequence
- Element : DefaultIfEmpty, ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
- Equality : SequenceEqual
- Generation : Empty, Range, Repeat
- Grouping : GroupBy
- Joining : GroupJoin, Join
- Ordering: OrderBy, ThenBy, OrderByDescending, ThenByDescending, Reverse
- Partitioning : Skip, SkipWhile, Take, TakeWhile
- Quantifiers: All, Any, Contains
- Restriction : Where
- Projection: Select, SelectMany
- Set: Distinct, Except, Intersect, Union
There are two sets of LINQ standard query operators, one that operates on objects of type IEnumerable<(Of <(T>)>) and the other that operates on objects of type IQueryable<(Of <(T>)>). The methods that make up each set are static members of the Enumerable and Queryable classes, respectively. 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.
In addition, several standard query operator methods operate on types other than those based on IEnumerable<(Of <(T>)>) or IQueryable<(Of <(T>)>). The Enumerable type defines two such methods that both operate on objects of type IEnumerable. These methods, Cast<(Of <(TResult>)>)(IEnumerable) and OfType<(Of <(TResult>)>)(IEnumerable),
let you enable a non-parameterized, or non-generic, collection to be
queried in the LINQ pattern. They do this by creating a strongly-typed
collection of objects. The Queryable class defines two similar methods, Cast<(Of <(TResult>)>)(IQueryable) and OfType<(Of <(TResult>)>)(IQueryable), that operate on objects of type Queryable.
The standard query operators differ in the timing of their
execution, depending on whether they return a singleton value or a
sequence of values. Those methods that return a singleton value (for
example, Average and Sum) execute immediately. Methods that return a sequence defer the query execution and return an enumerable object.
In the case of the methods that operate on in-memory collections, that is, those methods that extend IEnumerable<(Of <(T>)>),
the returned enumerable object captures the arguments that were passed
to the method. When that object is enumerated, the logic of the query
operator is employed and the query results are returned.
In contrast, methods that extend IQueryable<(Of <(T>)>)
do not implement any querying behavior, but build an expression tree
that represents the query to be performed. The query processing is
handled by the source IQueryable<(Of <(T>)>) object.
Example Using Standard Query Operators in LINQ
string sentence = "the quick brown fox jumps over the lazy dog";
// Split the string into individual words to create a collection.
string[] words = sentence.Split(' ');// Using query expression syntax.
var query = from word in words
group word.ToUpper() by word.Length into gr
orderby gr.Key
select new { Length = gr.Key, Words = gr };// Using method-based query syntax.
var query2 = words.
GroupBy(w => w.Length, w => w.ToUpper()).
Select(g => new { Length = g.Key, Words = g }).
OrderBy(o => o.Length);foreach (var obj in query)
{
Console.WriteLine("Words of length {0}:", obj.Length);
foreach (string word in obj.Words)
Console.WriteLine(word);
}// This code example produces the following output:
//
// Words of length 3:
// THE
// FOX
// THE
// DOG
// Words of length 4:
// OVER
// LAZY
// Words of length 5:
// QUICK
// BROWN
// JUMPS
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.

[...] 2008 at 2:22 and is filed under Hayoo!. Tags: operators, query. Leave a Reply. Name (required) …Standard Query Operators in LINQ | LINQ ExchangeThe standard query operators are the methods that form the Language-Integrated Query (LINQ) pattern. [...]