linq
  1. linq-filtering-and-sorting

Filtering and Sorting with LINQ to Objects

LINQ to Objects is a powerful tool for querying collections in .NET applications. In this tutorial, we'll explore how to filter and sort data using LINQ to Objects.

Filtering Data

Filtering in LINQ to Objects is done using the where clause. The where clause is used to filter out elements that don't match a certain condition. For example, suppose we have a list of integers and we want to filter out all the even numbers. We can achieve this using the following code:

List<int> lstNumbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var oddNumbers = lstNumbers.Where(n => n % 2 != 0);

In the above example, we created a list of integers and then used the Where() method to filter out all the even numbers from the list. The Where() method takes a predicate as an argument, which is a function that returns a Boolean value. If the predicate returns true, the item is included in the result; otherwise, it's excluded.

Example

Let's suppose we have a list of Person objects with Id, Name, Age, and Gender properties. We want to get all the male persons with age less than 30. We can achieve this using the following code:

List<Person> persons = new List<Person>()
{
    new Person{Id = 1, Name = "John", Age = 25, Gender = Gender.Male},
    new Person{Id = 2, Name = "Mary", Age = 35, Gender = Gender.Female},
    new Person{Id = 3, Name = "Bob", Age = 28, Gender = Gender.Male},
    new Person{Id = 4, Name = "Alice", Age = 40, Gender = Gender.Female}
};
var filteredPersons = persons.Where(p => p.Age < 30 && p.Gender == Gender.Male);

In the above example, we created a list of Person objects and then used the Where() method to filter out all the male persons with age less than 30.

Sorting Data

Sorting in LINQ to Objects is done using the OrderBy and OrderByDescending methods. The OrderBy method is used to sort the elements in ascending order, whereas OrderByDescending is used to sort the elements in descending order. For example, suppose we have a list of integers and we want to sort them in ascending order. We can do this using the following code:

List<int> lstNumbers = new List<int>() { 5, 2, 8, 1, 9, 4, 6, 3, 7 };
var sortedNumbers = lstNumbers.OrderBy(n => n);

In the above example, we created a list of integers and used the OrderBy() method to sort the elements in ascending order.

Example

Let's suppose we have a list of Person objects with Id, Name, Age, and Gender properties. We want to get all the persons sorted by their name in ascending order. We can achieve this using the following code:

List<Person> persons = new List<Person>()
{
    new Person{Id = 1, Name = "John", Age = 25, Gender = Gender.Male},
    new Person{Id = 2, Name = "Mary", Age = 35, Gender = Gender.Female},
    new Person{Id = 3, Name = "Bob", Age = 28, Gender = Gender.Male},
    new Person{Id = 4, Name = "Alice", Age = 40, Gender = Gender.Female}
};
var sortedPersons = persons.OrderBy(p => p.Name);

In the above example, we created a list of Person objects and then used the OrderBy() method to sort the elements by their name in ascending order.

Summary

In this tutorial, we've learned how to filter and sort data using LINQ to Objects in .NET applications. We've seen how to use the Where() method to filter out elements that don't match a certain condition, and the OrderBy() and OrderByDescending() methods to sort elements in ascending or descending order.

Published on: