Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Sorting): enable pre sorting in resolver #1253

Merged

Conversation

PascalSenn
Copy link
Member

Queryables can now be pre sorted in the resolver

@PascalSenn PascalSenn added this to the 10.3.0 milestone Dec 3, 2019
@PascalSenn
Copy link
Member Author

I'll check

@PascalSenn
Copy link
Member Author

@michaelstaib IOrderedQueryable behaves not as one might expect.

IQueryable<Foo> t = new Foo []{}.AsQueryable();
t is IOrderedQueryable<Foo>// true
typeof(IOrderedQueryable<Foo>).IsAssignableFrom(t.GetType()) // true

The Problem

https://stackoverflow.com/questions/5071426/how-can-i-tell-if-an-iqueryable-is-an-iorderedqueryable/22012961

The problem with Entity Framework is that EF DbSet inherits from DbQuery which itself implements IOrderedQueryable! So your statement always returns true if myQueryable is an EF query but not sorted at all.

https://stackoverflow.com/questions/8507746/determine-whether-an-iqueryablet-has-been-ordered-or-not

This doesn't seem to work on a collection like dbSet.AsQueryable() as this will always return true on the is IOrderedQueryable check. – Juri Jan 23 '12 at 13:50

A solution

Yes you can inspect the IQueryable.Expression tree to see if it calls any of the OrderBy/ThenBy methods. Expression trees can be examined by deriving a class from ExpressionVisitor.

There is an internal OrderingMethodFinder in System.Web - which you could adapt. Here's what I came up with:
https://stackoverflow.com/questions/8507746/determine-whether-an-iqueryablet-has-been-ordered-or-not

{
bool _orderingMethodFound = false;

public override Expression Visit(Expression node)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the reference implementation slightly. This way it stops once a ordermethod is found

var name = node.Method.Name;

if (node.Method.DeclaringType == typeof(Queryable) && (
name.StartsWith(nameof(Queryable.OrderBy), StringComparison.Ordinal) ||
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added nameof instead of magic strings

@michaelstaib michaelstaib merged commit 293058b into version_10_0_0_master Dec 4, 2019
@michaelstaib michaelstaib deleted the FEAT_enable-presorting-in-resolver branch December 4, 2019 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants