LINQ - Query Operators


Advertisements


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 capabilities like filtering, sorting, projection, aggregation, etc.

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

  • Filtering Operators
  • Join Operators
  • Projection Operations
  • Sorting Operators
  • Grouping Operators
  • Conversions
  • Concatenation
  • Aggregation
  • Quantifier Operations
  • Partition Operations
  • Generation Operations
  • Set Operations
  • Equality
  • 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

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
WhereFilter values based on a predicate functionwhereWhere
OfTypeFilter values based on their ability to be as a specified typeNot ApplicableNot Applicable

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

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
JoinThe operator join two sequences on basis of matching keysjoin … in … on … equals …From x In …, y In … Where x.a = y.a
GroupJoinJoin two sequences and group the matching elementsjoin … 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

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
SelectThe operator projects values on basis of a transform functionselectSelect
SelectManyThe operator project the sequences of values which are based on a transform function as well as flattens them into a single sequenceUse multiple from clausesUse 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

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
OrderByThe operator sort values in an ascending orderorderbyOrder By
OrderByDescendingThe operator sort values in a descending orderorderby ... descendingOrder By ... Descending
ThenByExecutes a secondary sorting in an ascending orderorderby …, …Order By …, …
ThenByDescendingExecutes a secondary sorting in a descending orderorderby …, … descendingOrder By …, … Descending
ReversePerforms a reversal of the order of the elements in a collectionNot ApplicableNot Applicable

Grouping Operators

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

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
GroupByOrganize 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 …
ToLookupExecute a grouping operation in which a sequence of key pairs are returnedNot ApplicableNot Applicable

Conversions

The operators change the type of input objects and are used in a diverse range of applications.

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
AsEnumerableReturns the input typed as IEnumerable<T>Not ApplicableNot Applicable
AsQueryableA (generic) IEnumerable is converted to a (generic) IQueryableNot ApplicableNot Applicable
CastPerforms casting of elements of a collection to a specified typeUse an explicitly typed range variable. Eg:from string str in wordsFrom … As …
OfTypeFilters values on basis of their , depending on their capability to be cast to a particular typeNot ApplicableNot Applicable
ToArrayForces query execution and does conversion of a collection to an arrayNot ApplicableNot Applicable
ToDictionaryOn basis of a key selector function set elements into a Dictionary<TKey, TValue> and forces execution of a LINQ queryNot ApplicableNot Applicable
ToListForces execution of a query by converting a collection to a List<T>Not ApplicableNot Applicable
ToLookupForces execution of a query and put elements into a Lookup<TKey, TElement> on basis of a key selector functionNot ApplicableNot Applicable

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 duplicates.

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
ConcatTwo sequences are concatenated for the formation of a single one sequence.Not ApplicableNot Applicable

Aggregation

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

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
AggregateOperates on the values of a collection to perform custom aggregation operationNot ApplicableNot Applicable
AverageAverage value of a collection of values is calculatedNot ApplicableAggregate … In … Into Average()
CountCounts the elements satisfying a predicate function within collectionNot ApplicableAggregate … In … Into Count()
LonCountCounts the elements satisfying a predicate function within a huge collectionNot ApplicableAggregate … In … Into LongCount()
MaxFind out the maximum value within a collectionNot ApplicableAggregate … In … Into Max()
MinFind out the minimum value existing within a collectionNot ApplicableAggregate … In … Into Min()
SumFind out the sum of a values within a collectionNot ApplicableAggregate … 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

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
AllReturns a value ‘True’ if all elements of a sequence satisfy a predicate conditionNot ApplicableAggregate … In … Into All(…)
AnyDetermines by searching a sequence that whether any element of the same satisfy a specified conditionNot ApplicableAggregate … In … Into Any()
ContainsReturns 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 returnedNot ApplicableNot Applicable

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

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
SkipSkips some specified number of elements within a sequence and returns the remaining onesNot ApplicableSkip
SkipWhileSame as that of Skip with the only exception that number of elements to skip are specified by a Boolean conditionNot ApplicableSkip While
TakeTake a specified number of elements from a sequence and skip the remaining onesNot ApplicableTake
TakeWhileSame as that of Take except the fact that number of elements to take are specified by a Boolean conditionNot ApplicableTake While

Generation Operations

A new sequence of values is created by generational operators.

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
DefaultIfEmptyWhen applied to an empty sequence, generate a default element within a sequenceNot ApplicableNot Applicable
EmptyReturns an empty sequence of values and is the most simplest generational operatorNot ApplicableNot Applicable
RangeGenerates a collection having a sequence of integers or numbersNot ApplicableNot Applicable
RepeatGenerates a sequence containing repeated values of a specific lengthNot ApplicableNot Applicable

Set Operations

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

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
DistinctResults a list of unique values from a collection by filtering duplicate data if anyNot ApplicableDistinct
ExceptCompares the values of two collections and return the ones from one collection who are not in the other collectionNot ApplicableNot Applicable
IntersectReturns the set of values found t be identical in two separate collectionsNot ApplicableNot Applicable
UnionCombines content of two different collections into a single list that too without any duplicate contentNot ApplicableNot Applicable

Equality

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

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
SequenceEqualResults a Boolean value if two sequences are found to be identical to each otherNot ApplicableNot Applicable

Element Operators

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

Show Examples

OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
ElementAtReturns an element present within a specific index in a collectionNot ApplicableNot Applicable
ElementAtOrDefaultSame as ElementAt except of the fact that it also returns a default value in case the specific index is out of rangeNot ApplicableNot Applicable
FirstRetrieves the first element within a collection or the first element satisfying a specific conditionNot ApplicableNot Applicable
FirstOrDefaultSame as First except the fact that it also returns a default value in case there is no existence of such elementsNot ApplicableNot Applicable
LastRetrieves the last element present in a collection or the last element satisfying a specific conditionNot ApplicableNot Applicable
LastOrDefaultSame as Last except the fact that it also returns a default value in case there is no existence of any such elementNot ApplicableNot Applicable
SingleReturns the lone element of a collection or the lone element that satisfy a certain conditionNot ApplicableNot Applicable
SingleOrDefaultSame as Single except that it also returns a default value if there is no existence of any such lone elementNot ApplicableNot Applicable
DefaultIfEmptyReturns a default value if the collection or list is empty or nullNot ApplicableNot Applicable


Advertisements