What happens when two items have the same 'Primary' key? ThenBy allows you to specify subsequent sorting criteria.
A common mistake is calling .OrderBy(x => x.A).OrderBy(x => x.B). This actually throws away the first sort and re-sorts the entire list by B. To maintain the primary sort, you MUST use ThenBy.
// Correct way: Sort by LastName, then by FirstName
var sortedUsers = users.OrderBy(u => u.LastName)
.ThenBy(u => u.FirstName);
You can chain as many ThenBy or ThenByDescending calls as you need. C# handles the priority from left to right. This is essential for building complex data tables with multi-column sorting.
Q: "Do multiple levels of sorting impact performance?"
Architect Answer: "Yes, each level adds a small amount of comparison logic. However, the biggest impact is on the database. In SQL, this becomes ORDER BY A, B, C. Ensure your DB composite indexes match your most common multi-column search patterns to keep queries optimized."