What is ThenBy in LINQ?

ThenBy() is a technique used in LINQ (Language Integrated Query) when we want to perform sorting on multiple fields. It’s used in combination with the OrderBy() function.

Use of the ThenBy() method

The collection is sorted using the ascending OrderBy() function according to the provided field. To arrange the collection on a different field in ascending order, use the ThenBy() function after OrderBy. The collection will first be sorted using the main field indicated by the OrderBy method, and the collection will then be sorted once again in ascending order using the secondary field supplied by the ThenBy method.

Syntax

Here’s the syntax to use ThenBy in combination with OrderBy:

products.OrderBy(product => product.Category).ThenBy(product => product.Price); 

The products are the starting collection in this scenario, and we’re sorting them by category and price. We may modify the characteristics and criteria to suit our data and sorting needs.

Code example

Let’s demonstrate the use of ThenBy with the following example:

using System;
using System.Linq;
using System.Collections.Generic;
public class personProfile
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
}
class Test
{
static void Main(string[] args)
{
List<personProfile> peopleList = new List<personProfile>
{
new personProfile { firstName = "Johnny", lastName = "Depp", age = 45 },
new personProfile { firstName = "Arthur", lastName = "Chipping", age = 40 },
new personProfile { firstName = "Katherine", lastName = "Bridges", age = 30 },
};
var sortedPeopleProfile = peopleList
.OrderBy(p => p.lastName)
.ThenBy(p => p.firstName)
.ThenBy(p => p.age);
foreach (var p in sortedPeopleProfile)
{
Console.WriteLine($"{p.firstName}, {p.lastName}, {p.age}");
}
}
}

Explanation

  • Lines 5–10: Define a class named personProfile with three properties: firstName, lastName, and age. These properties represent a person’s last name, first name, and age. The get; set; notation indicates that these properties have both getter and setter methods, allowing us to read and modify their values.
  • Lines 12–21: Define a class named Test with a static method named Main, which serves as the program’s entry point. Inside Main, a List<personProfile> named peopleList is created and initialized with three personProfile objects. These objects represent different individuals with their last name, first name, and age.
  • Lines 23–26: Declare a new variable named sortedPeopleProfile. It’s assigned the result of sorting the peopleList using LINQ’s OrderBy and ThenBy methods. The sorting is performed in multiple steps: first by lastName, then by firstName, and finally by age. The use of method chaining ensures that the sorting is performed in the specified order.
  • Lines 28–31: This loop iterates through each personProfile object in the sortedPeopleProfile collection (which is the result of the sorting). Inside the loop, the program prints out the last name, first name, and age of each person using the Console.WriteLine method.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved