LINQ | Sorting Operator | ThenBy
In LINQ, sorting operators are used to rearrange the given sequence in ascending or descending order based on one or more attributes. There are 5 different types of sorting operators are available in LINQ:
ThenBy operator is used to implementing secondary sort in ascending order. The multiple sorting is supported by ThenBy operator. Generally, ThenBy method is used with the OrderBy method. The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order. Or in other words, in LINQ, the collection is the first sort according to the primary field which is given by OrderBy Method after that the result of the primary sort is again sorted by the secondary field that is given by the ThenBy method.
- It does not support query syntax in C# and VB.Net languages.
- It support method syntax in both C# and VB.Net languages.
- It present in both the Queryable and Enumerable class.
- The secondary sorting in query syntax is separated by using comma as shown in Example 1.
- It is implemented by using deferred execution.
Employee Name: Anil Gender: Male Employee Name: Anjita Gender: Female Employee Name: Anju Gender: Female Employee Name: Rohit Gender: Male Employee Name: Soniya Gender: Female Employee Name: Supriya Gender: Female
Employee Name: Anil Salary: 40000 Employee Name: Anjita Salary: 20000 Employee Name: Anju Salary: 50000 Employee Name: Rohit Salary: 40000 Employee Name: Soniya Salary: 30000 Employee Name: Supriya Salary: 40000