OrderBy and ThenBy problem
See original GitHub issueI know that this came from EF6, but it is ugly construction. It is very difficult to do chain of method with conditions:
var items = db.Items;
if (condition1)
{
items = items.OrderBy(x => x.Field1);
}
if (condition2)
{
items = items.ThenBy(x => x.Field2);
}
if condition1
is false, then ThenBy throw exception. There are no really need to call OrderBy
first and ThenBy
next. More convinient would be method AddOrderBy
instead of that two. Or in addition for them.
Issue Analytics
- State:
- Created 7 years ago
- Comments:16 (4 by maintainers)
Top Results From Across the Web
OrderBy().ThenBy().ThenBy() not giving expected result on ...
Having trouble understanding why my simple ordering doesn't work as expected. Here's the offending line: return messages.OrderBy(o => o.
Read more >OrderBy and ThenBy problem · Issue #7233 · dotnet/efcore
I know that this came from EF6, but it is ugly construction. It is very difficult to do chain of method with conditions:...
Read more >Sorting in C#: OrderBy.OrderBy or OrderBy.ThenBy? ...
Suppose we need to sort the collection by multiple keys. In C#, we can do this with the help of OrderBy().OrderBy() or OrderBy().ThenBy()....
Read more >LINQ ThenBy and ThenByDescending Method in C# with ...
The Linq ThenBy Method in C# is used to sort the data in Ascending order from the second level onwards. On the other...
Read more >[Solved]-LINQ OrderBy().ThenBy() not working-LINQ,C#
They haven't provided a ThenBy method, but instead they're collecting multiple orderings with multiple OrderBy calls; They haven't exposed the table as an ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@anpete @MichaelKetting reading the whole thread I tend agree that the fact that queries returned from all operators are
IOrderedQueryable<T>
is less than ideal. Any thoughts on this?I tried it and this is not work.
DbQuery<T>
realize bothIQuerable<T>
andIOrderedQuerable<T>
. There are no way to realize was OrderBy called or not.Magic of OrderBy -> ThenBy work on interfaces that they return and extend (as extensition method). It work only if you call them in one chain
var items = db.Items.OrderBy(x => x.Time).ThenBy(x => x.Name)
and broke in this scenario: