LINQ Interview Question #36
What is the difference between ThenBy and another OrderBy?
Filtering, Projection, Ordering & Element Operators Junior Beginner
Quick Interview Answer
ThenBy adds a secondary ascending sort to an existing ordering. Another OrderBy starts a new primary ordering and can replace the significance of the previous ordering.
Detailed Explanation
ThenBy adds a secondary ascending sort to an existing ordering. Another OrderBy starts a new primary ordering and can replace the significance of the previous ordering.
Use ThenBy or ThenByDescending when records with the same primary key need a predictable secondary order.
Code Example
var result = employees
.OrderBy(e => e.Department)
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);