Repository navigation
Order collection navigations loaded by a Select projection - #184
Merged
Merged
Conversation
A collection navigation read inside a Select projection is loaded by a subquery of its own. Nothing ordered it, so EF Core returned its rows in key order and the configured default ordering was ignored. Only Include() collections got it. ProjectionOrderingApplicator orders each unordered collection navigation in a Select lambda by its entity's default, after any Where and before the operators that project the entity away or choose which rows come back. A navigation bound into the result as it is is ordered too. Collections only aggregated, or composed with an operator EF Core drops ordering beneath such as Distinct, are left alone. Explicit ordering in a projection is kept and checked by redundant ordering detection. Version bumped to 1.4.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Collection navigations read inside a
Selectprojection are now ordered by their entity's default ordering, the same as collections in anInclude().A projected collection, such as
Select(_ => new { Employees = _.Employees.Where(...).Select(...).ToList() }), is loaded by a subquery of its own. Nothing ordered it, so EF Core returned its rows in key order and the configured default was ignored. GraphQL.EntityFramework loads navigation list fields this way, so a client that left outorderBybecause it matched the model's default got key order instead, which for a Guid key looks random.ProjectionOrderingApplicatorvisits the lambdas ofQueryable.Selectand orders each unordered collection navigation it finds:Whereand beforeSelect,Skip,Take,First,FirstOrDefault,Last,LastOrDefault,ElementAt,ElementAtOrDefault,ToList,ToArrayandAsEnumerablenew { _.Employees }, is ordered and copied into a list, where a list fits the member's typeCount,Any,Sumand the like), or composed with an operator EF Core drops ordering beneath such asDistinct, is left alone, as is the second operand of a set operationInclude()Include()This changes the SQL of any query that projects a collection navigation without ordering it: the subquery gains the default
ORDER BY. WiththrowOnRedundantOrderByon, an explicit ordering in a projection that matches the default now throws.Tests cover each case in
ProjectionOrderingTests, and redundant detection in projections inRedundantOrderByTests. One existing snapshot changed:DefaultOrdering_WithIncludeAndSelect_WithNavInSelect, whose bound_.Employeesnow getsHireDate DESC.Documented in the readme. Version bumped to 1.4.0.
GraphQL.EntityFramework orders projected navigation lists by key unless told not to. SimonCropp/GraphQL.EntityFramework#1458 adds the option that lets this default apply instead.