Javatpoint Logo
Javatpoint Logo

LINQ ThenBy Operator

ThenBy sorting operator is used to implement the sorting on multiple fields, and by default, ThenBy operator will sort the collection of items in ascending order. Generally, in LINQ ThenBy operator is used along with OrderBy operator to implement the sorting on multiple fields in the list/collection.

If we want more than one condition on sorting in LINQ, then we use the ThenBy clause with the OrderBy clause. In LINQ, OrderBy is the primary sorting operator, and ThenBy is the secondary operator.

Syntax of LINQ ThenBy Operator

The Syntax of using the ThenBy operator in LINQ to implement the sorting on multiple fields is:

In the above Syntax, we are sorting the list of items using "Name," and we added another field "Roleid" by using the ThenBy condition to sort the list of items. Now, we will see this with the help of an example.

Example of LINQ ThenBy operator

Here is the example of LINQ ThenBy operator to sort the list/collection of items which is based on multiple fields.

C# Code

In the above example, we are sorting the "ObjStudent" list item using the multiple fields Name, RoleNumber Id.

OUTPUT:

The result of LINQ ThenBy sorting operator to sort the list of items based on the multiple fields is:

LINQ ThenBy Operator




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA