Order by Col1, Col2 using entity framework
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Entity Framework, ordering by multiple columns is done with OrderBy for the first sort key and ThenBy for each additional key. This maps cleanly to SQL ORDER BY col1, col2. The important detail is that a second OrderBy replaces the first sort, while ThenBy extends it.
Basic Multi-Column Ordering
Suppose you have products and want them sorted by category first and then by price.
That translates to the SQL idea of:
ORDER BY Category ASC, Price ASC
Use OrderByDescending and ThenByDescending when needed:
Now the primary sort is still category ascending, but rows inside each category are sorted by price descending.
Why ThenBy Matters
A common mistake is chaining two OrderBy calls:
The second OrderBy does not "add" to the first one. It starts a new ordering. In practice, the final result is ordered by price only.
The correct form is:
That distinction is one of the most common LINQ sorting bugs.
Paging Requires Deterministic Ordering
If you use Skip and Take, a stable multi-column order becomes even more important.
Including a unique final tie-breaker such as Id helps keep pagination deterministic. Without that, rows with identical sort values can move between pages as data changes.
Ordering on Related Data
You can also order by properties from related entities if EF can translate the navigation access.
This is common in reporting screens. Just remember that sort complexity still affects the generated SQL and database execution plan.
Dynamic Ordering Still Needs Care
If users can choose sort columns at runtime, build the expression carefully. A simple branch-based approach is often clearer than trying to invent complex string-driven magic too early.
This keeps the generated query explicit and easy to debug.
Performance Considerations
Entity Framework can translate multi-column ordering well, but the database still has to execute it. Sorting large result sets can be expensive, especially if the ordered columns are not indexed.
Good practices:
- index columns frequently used for sorting
- project only the columns you need
- combine filtering before ordering when possible
- use deterministic ordering for paged queries
Example projection:
That reduces payload size and keeps the query focused.
Common Pitfalls
- Using a second
OrderBywhenThenBywas intended. - Forgetting a stable final tie-breaker when doing pagination.
- Assuming EF ordering rules differ from normal LINQ ordering semantics.
- Sorting on large data sets without considering indexes.
- Building dynamic ordering in a way that obscures the generated SQL.
Summary
- In Entity Framework, use
OrderByfor the first key andThenByfor additional keys. - A second
OrderByreplaces the previous order instead of extending it. - Use descending variants where required.
- Add a deterministic final sort key for paging.
- Think about indexes and query shape because ordering cost is still paid in the database.
Related reading
- Order by in DynamoDB using params
- Ordering by specific field value first
- Ordering by the order of values in a SQL IN clause
- org.apache.spark.sql.AnalysisException Can't extract value from probability
- Ordering array by dependencies with perl
- Ordinal classification packages and algorithms
- Overhead of try/finally in C?
- Override Authorize Attribute in ASP.NET MVC

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.