question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OrderBy and ThenBy problem

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
divegacommented, Jan 21, 2017

@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?

1reaction
justseregacommented, Dec 12, 2016

You can do that yourself

I tried it and this is not work. DbQuery<T> realize both IQuerable<T> and IOrderedQuerable<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:

var items = db.Items.AsQueryable();
items = items.OrderBy(x => x.Time);
items = items.ThenBy(x => x.Name); // oops, it is not compiled
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found