English 中文(简体)
LINQ - Query Operators
  • 时间:2024-11-05

LINQ - Query Operators


Previous Page Next Page  

A set of extension methods forming a query pattern is known as LINQ Standard Query Operators. As building blocks of LINQ query expressions, these operators offer a range of query capabipties pke filtering, sorting, projection, aggregation, etc.

LINQ standard query operators can be categorized into the following ones on the basis of their functionapty.

    Filtering Operators

    Join Operators

    Projection Operations

    Sorting Operators

    Grouping Operators

    Conversions

    Concatenation

    Aggregation

    Quantifier Operations

    Partition Operations

    Generation Operations

    Set Operations

    Equapty

    Element Operators

Filtering Operators

Filtering is an operation to restrict the result set such that it has only selected elements satisfying a particular condition.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
where Filter values based on a predicate function where Where
OfType Filter values based on their abipty to be as a specified type Not Apppcable Not Apppcable

Join Operators

Joining refers to an operation in which data sources with difficult to follow relationships with each other in a direct way are targeted.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Join The operator join two sequences on basis of matching keys join … in … on … equals … From x In …, y In … Where x.a = y.a
GroupJoin Join two sequences and group the matching elements join … in … on … equals … into … Group Join … In … On …

Projection Operations

Projection is an operation in which an object is transformed into an altogether new form with only specific properties.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Select The operator projects values on basis of a transform function select Select
SelectMany The operator project the sequences of values which are based on a transform function as well as flattens them into a single sequence Use multiple from clauses Use multiple From clauses

Sorting Operators

A sorting operation allows ordering the elements of a sequence on basis of a single or more attributes.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
OrderBy The operator sort values in an ascending order orderby Order By
OrderByDescending The operator sort values in a descending order orderby ... descending Order By ... Descending
ThenBy Executes a secondary sorting in an ascending order orderby …, … Order By …, …
ThenByDescending Executes a secondary sorting in a descending order orderby …, … descending Order By …, … Descending
Reverse Performs a reversal of the order of the elements in a collection Not Apppcable Not Apppcable

Grouping Operators

The operators put data into some groups based on a common shared attribute.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
GroupBy Organize a sequence of items in groups and return them as an IEnumerable collection of type IGrouping<key, element> group … by -or- group … by … into … Group … By … Into …
ToLookup Execute a grouping operation in which a sequence of key pairs are returned Not Apppcable Not Apppcable

Conversions

The operators change the type of input objects and are used in a spanerse range of apppcations.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
AsEnumerable Returns the input typed as IEnumerable<T> Not Apppcable Not Apppcable
AsQueryable A (generic) IEnumerable is converted to a (generic) IQueryable Not Apppcable Not Apppcable
Cast Performs casting of elements of a collection to a specified type Use an exppcitly typed range variable. Eg:from string str in words From … As …
OfType Filters values on basis of their , depending on their capabipty to be cast to a particular type Not Apppcable Not Apppcable
ToArray Forces query execution and does conversion of a collection to an array Not Apppcable Not Apppcable
ToDictionary On basis of a key selector function set elements into a Dictionary<TKey, TValue> and forces execution of a LINQ query Not Apppcable Not Apppcable
ToList Forces execution of a query by converting a collection to a List<T> Not Apppcable Not Apppcable
ToLookup Forces execution of a query and put elements into a Lookup<TKey, TElement> on basis of a key selector function Not Apppcable Not Apppcable

Concatenation

Performs concatenation of two sequences and is quite similar to the Union operator in terms of its operation except of the fact that this does not remove duppcates.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Concat Two sequences are concatenated for the formation of a single one sequence. Not Apppcable Not Apppcable

Aggregation

Performs any type of desired aggregation and allows creating custom aggregations in LINQ.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Aggregate Operates on the values of a collection to perform custom aggregation operation Not Apppcable Not Apppcable
Average Average value of a collection of values is calculated Not Apppcable Aggregate … In … Into Average()
Count Counts the elements satisfying a predicate function within collection Not Apppcable Aggregate … In … Into Count()
LonCount Counts the elements satisfying a predicate function within a huge collection Not Apppcable Aggregate … In … Into LongCount()
Max Find out the maximum value within a collection Not Apppcable Aggregate … In … Into Max()
Min Find out the minimum value existing within a collection Not Apppcable Aggregate … In … Into Min()
Sum Find out the sum of a values within a collection Not Apppcable Aggregate … In … Into Sum()

Quantifier Operations

These operators return a Boolean value i.e. True or False when some or all elements within a sequence satisfy a specific condition.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
All Returns a value ‘True’ if all elements of a sequence satisfy a predicate condition Not Apppcable Aggregate … In … Into All(…)
Any Determines by searching a sequence that whether any element of the same satisfy a specified condition Not Apppcable Aggregate … In … Into Any()
Contains Returns a ‘True’ value if finds that a specific element is there in a sequence if the sequence doe not contains that specific element , ‘false’ value is returned Not Apppcable Not Apppcable

Partition Operators

Divide an input sequence into two separate sections without rearranging the elements of the sequence and then returning one of them.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Skip Skips some specified number of elements within a sequence and returns the remaining ones Not Apppcable Skip
SkipWhile Same as that of Skip with the only exception that number of elements to skip are specified by a Boolean condition Not Apppcable Skip While
Take Take a specified number of elements from a sequence and skip the remaining ones Not Apppcable Take
TakeWhile Same as that of Take except the fact that number of elements to take are specified by a Boolean condition Not Apppcable Take While

Generation Operations

A new sequence of values is created by generational operators.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
DefaultIfEmpty When appped to an empty sequence, generate a default element within a sequence Not Apppcable Not Apppcable
Empty Returns an empty sequence of values and is the most simplest generational operator Not Apppcable Not Apppcable
Range Generates a collection having a sequence of integers or numbers Not Apppcable Not Apppcable
Repeat Generates a sequence containing repeated values of a specific length Not Apppcable Not Apppcable

Set Operations

There are four operators for the set operations, each yielding a result based on different criteria.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Distinct Results a pst of unique values from a collection by filtering duppcate data if any Not Apppcable Distinct
Except Compares the values of two collections and return the ones from one collection who are not in the other collection Not Apppcable Not Apppcable
Intersect Returns the set of values found t be identical in two separate collections Not Apppcable Not Apppcable
Union Combines content of two different collections into a single pst that too without any duppcate content Not Apppcable Not Apppcable

Equapty

Compares two sentences (enumerable ) and determine are they an exact match or not.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
SequenceEqual Results a Boolean value if two sequences are found to be identical to each other Not Apppcable Not Apppcable

Element Operators

Except the DefaultIfEmpty, all the rest eight standard query element operators return a single element from a collection.

Show Examples

Operator Description C# Query Expression Syntax VB Query Expression Syntax
ElementAt Returns an element present within a specific index in a collection Not Apppcable Not Apppcable
ElementAtOrDefault Same as ElementAt except of the fact that it also returns a default value in case the specific index is out of range Not Apppcable Not Apppcable
First Retrieves the first element within a collection or the first element satisfying a specific condition Not Apppcable Not Apppcable
FirstOrDefault Same as First except the fact that it also returns a default value in case there is no existence of such elements Not Apppcable Not Apppcable
Last Retrieves the last element present in a collection or the last element satisfying a specific condition Not Apppcable Not Apppcable
LastOrDefault Same as Last except the fact that it also returns a default value in case there is no existence of any such element Not Apppcable Not Apppcable
Single Returns the lone element of a collection or the lone element that satisfy a certain condition Not Apppcable Not Apppcable
SingleOrDefault Same as Single except that it also returns a default value if there is no existence of any such lone element Not Apppcable Not Apppcable
DefaultIfEmpty Returns a default value if the collection or pst is empty or null Not Apppcable Not Apppcable
Advertisements