LINQ Operators

LINQ Operators in C#

In this article, I will discuss the LINQ operators in C#. Please read our previous article discussing the LINQ Extension methods and how they are implemented. As part of this article, we will discuss the following two points.

LINQ Operators in C#

What are LINQ Operators?

The LINQ Operators are nothing but a set of extension methods used to write the LINQ Query. These LINQ extension methods provide many useful features we can apply to the data source. Some of the features are filtering the data, sorting the data, grouping the data, etc.

What are the Categories of LINQ Operators?

LINQ (Language-Integrated Query) operators in C# provide a way to query and manipulate data from arrays, enumerable classes, XML, relational databases, and third-party data sources. The operators are divided into different categories based on their functionality:

Projection Operators:
  • Select: Projects each element of a sequence into a new form.
  • SelectMany: Projects each sequence element to an IEnumerable<T> and flattens the resulting sequences into one sequence.
Restriction Operators:
  • Where: Filters a sequence of values based on a predicate.
Partitioning Operators:
  • Take: Returns a specified number of contiguous elements from the start of a sequence.
  • Skip: Bypasses a specified number of elements in a sequence and then returns the remaining elements.
  • TakeWhile: Returns elements from a sequence as long as a specified condition is true.
  • SkipWhile: Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
Ordering Operators:
  • OrderBy: Sorts the elements of a sequence in ascending order according to a key.
  • OrderByDescending: Sorts the elements of a sequence in descending order according to a key.
  • ThenBy: Performs a subsequent ordering of the elements in a sequence in ascending order.
  • ThenByDescending: Performs a subsequent ordering of the elements in a sequence in descending order.
  • Reverse: Inverts the order of the elements in a sequence.
Grouping Operators:
  • GroupBy: Groups the elements of a sequence according to a specified key selector function.
Set Operators:
  • Distinct: Removes duplicate elements from a sequence.
  • Union: Produces the set union of two sequences.
  • Intersect: Produces the set intersection of two sequences.
  • Except: Produces the set difference of two sequences.
Conversion Operators:
  • AsEnumerable: Casts an IEnumerable to an IEnumerable<T>.
  • ToArray: Converts a sequence to an array.
  • ToList: Converts a sequence to a List<T>.
  • ToDictionary: Converts a sequence to a Dictionary<TKey, TValue> based on a key selector function.
  • OfType: Filters the elements of an IEnumerable based on a specified type.
Element Operators:
  • First: Returns the first element of a sequence.
  • FirstOrDefault: Returns the first element of a sequence or a default value if no element is found.
  • Last: Returns the last element of a sequence.
  • LastOrDefault: Returns the last element of a sequence or a default value if no element is found.
  • Single: Returns the only element of a sequence and throws an exception if there is not exactly one element in the sequence.
  • SingleOrDefault: Returns the only element of a sequence or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
  • ElementAt: Returns the element at a specified index in a sequence.
  • ElementAtOrDefault: Returns the element at a specified index in a sequence or a default value if the index is out of range.
Quantifiers:
  • Any: Determines whether any element of a sequence satisfies a condition.
  • All: Determines whether all elements of a sequence satisfy a condition.
  • Contains: Determines whether a sequence contains a specified element.
Aggregate Operators:
  • Count: Counts the elements in a sequence.
  • LongCount: Counts the elements in a sequence, returning the count as a long.
  • Sum: Computes the sum of a sequence of numeric values.
  • Min: Returns the minimum value in a sequence.
  • Max: Returns the maximum value in a sequence.
  • Average: Computes the average of a sequence of numeric values.
  • Aggregate: Applies an accumulator function over a sequence.
Equality Operators:
  • SequenceEqual: Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.
Generation Operators:
  • Empty: Returns an empty IEnumerable<T> with the specified type argument.
  • Repeat: Generates a sequence that contains one repeated value.
  • Range: Generates a sequence of integral numbers within a specified range.

In the next article, I will discuss Projection Operations with examples.

Leave a Reply

Your email address will not be published. Required fields are marked *