LINQ Operators

LINQ (Language-Integrated Query) is a powerful feature of C# that enables developers to easily manipulate data in various formats. One of the most important features of LINQ is the collection of operators that it provides. These operators are used to perform various operations on data, such as filtering, sorting, grouping, and aggregation. In this lesson, we will explore the different types of LINQ operators in C#.

  1. Filtering Operators:

Filtering operators are used to select a subset of elements from a collection based on certain criteria. Examples of filtering operators include Where, OfType, and TakeWhile.

  1. Sorting Operators:

Sorting operators are used to sort the elements of a collection based on certain criteria. Examples of sorting operators include OrderBy, OrderByDescending, and ThenBy.

  1. Grouping Operators:

Grouping operators are used to group elements of a collection based on a specified key. Examples of grouping operators include GroupBy and ToLookup.

  1. Join Operators:

Join operators are used to join two or more collections based on a specified key. Examples of join operators include Join, GroupJoin, and Zip.

  1. Projection Operators:

Projection operators are used to transform the elements of a collection into a new form. Examples of projection operators include Select, SelectMany, and Cast.

  1. Aggregation Operators:

Aggregation operators are used to perform calculations on a collection of elements. Examples of aggregation operators include Count, Sum, Average, Min, and Max.

Example:

Let’s take an example of a collection of customer objects:
List<Customer> customers = new List<Customer>()
{
    new Customer {Id = 1, FirstName = "John", LastName = "Doe"},
    new Customer {Id = 2, FirstName = "Jane", LastName = "Doe"},
    new Customer {Id = 3, FirstName = "Bob", LastName = "Smith"}
};
Using LINQ operators, we can perform various operations on this collection:
// Filter customers whose first name is "John"
IEnumerable<Customer> filteredCustomers = customers.Where(c => c.FirstName == "John");

// Sort customers by last name
IEnumerable<Customer> sortedCustomers = customers.OrderBy(c => c.LastName);

// Group customers by first name
IEnumerable<IGrouping<string, Customer>> groupedCustomers = customers.GroupBy(c => c.FirstName);

// Join customers with orders by customer ID
IEnumerable<CustomerOrder> customerOrders = customers.Join(orders, c => c.Id, o => o.CustomerId, (c, o) => new CustomerOrder { Customer = c, Order = o });

// Transform customers into a list of full names
IEnumerable<string> customerNames = customers.Select(c => $"{c.FirstName} {c.LastName}");

// Calculate the total number of orders for all customers
int totalOrders = customers.Sum(c => c.Orders.Count);
Conclusion:

In conclusion, LINQ operators are a powerful feature of C# that enable developers to easily manipulate data in various formats. By mastering these operators, developers can perform complex operations on data with ease and efficiency. Understanding the different types of LINQ operators and how to use them effectively is essential for any C# developer.