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
personProfilewith three properties:firstName,lastName, andage. These properties represent a person’s last name, first name, and age. Theget; 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
Testwith a static method namedMain, which serves as the program’s entry point. InsideMain, aList<personProfile>namedpeopleListis created and initialized with threepersonProfileobjects. 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 thepeopleListusing LINQ’sOrderByandThenBymethods. The sorting is performed in multiple steps: first bylastName, then byfirstName, and finally byage. The use of method chaining ensures that the sorting is performed in the specified order. - Lines 28–31: This loop iterates through each
personProfileobject in thesortedPeopleProfilecollection (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 theConsole.WriteLinemethod.
Free Resources