Skip to main content

LINQ Operators

Introduction to LINQ Operators

LINQ (Language Integrated Query) is a powerful feature in C# that allows you to work with data in a more intuitive and flexible way. It includes a set of operators, which are extension methods that provide the functionality to retrieve data from different data sources.

In this tutorial, we will explore different LINQ operators available in C#. These operators can be categorized into different types, including Filtering, Sorting, Grouping, Joining, Selecting, and Partitioning operators.

Filtering Operators

Filtering operators are used to filter out the elements from a collection based on some conditions. There are three types of filtering operators: Where, OfType, and Filter.

Let's see an example of Where operator:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = numbers.Where(n => n > 5);

In this example, Where operator will filter out the numbers that are greater than 5.

Sorting Operators

Sorting operators are used to sort the elements of a collection in a particular order. The two primary sorting operators are OrderBy and OrderByDescending.

Here is an example of OrderBy operator:

List<string> names = new List<string> { "John", "Bob", "Alice", "David" };
var sortedNames = names.OrderBy(n => n);

In this example, OrderBy operator will sort the names in ascending order.

Grouping Operators

Grouping operators are used to group the elements of a collection based on a specified key value. The key GroupBy operator is used for this purpose.

Here is an example of GroupBy operator:

List<string> names = new List<string> { "John", "Bob", "Alice", "David", "Alice", "John" };
var groupedNames = names.GroupBy(n => n);

In this example, GroupBy operator will group the names based on their values.

Joining Operators

Joining operators are used to combine elements from two collections based on a common key. The two primary joining operators are Join and GroupJoin.

Here is an example of Join operator:

List<string> names = new List<string> { "John", "Bob", "Alice", "David" };
List<int> ages = new List<int> { 30, 20, 25, 35 };
var result = names.Join(ages, n => n.Length, a => a, (n, a) => n + ": " + a);

In this example, Join operator will combine names and ages based on the length of names and the value of ages.

Selecting Operators

Selecting operators are used to project elements from a collection into a new form. The key Select operator is used for this purpose.

Here is an example of Select operator:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var squares = numbers.Select(n => n * n);

In this example, Select operator will project each number in the collection to its square.

Partitioning Operators

Partitioning operators are used to divide a collection into two parts. The two key partitioning operators are Take and Skip.

Here is an example of Take operator:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var firstThreeNumbers = numbers.Take(3);

In this example, Take operator will take the first three numbers from the collection.

In conclusion, LINQ operators in C# offer a powerful and flexible way to work with data. By understanding and using these operators, you can write more efficient and readable code. So, start experimenting with these operators and see how they can make your data manipulation tasks easier.