LINQ Interview Question #37

What does ThenByDescending do?

Filtering, Projection, Ordering & Element Operators Junior Beginner

Quick Interview Answer

ThenByDescending adds a secondary or later descending ordering to an existing ordered sequence. It is used after OrderBy, OrderByDescending, ThenBy, or another compatible ordered operation.

Detailed Explanation

ThenByDescending adds a secondary or later descending ordering to an existing ordered sequence. It is used after OrderBy, OrderByDescending, ThenBy, or another compatible ordered operation.

For example, records can be ordered by category ascending and then price descending within each category.

Code Example

var result = products
    .OrderBy(p => p.CategoryName)
    .ThenByDescending(p => p.Price);