Skip to content

Query: Order by bool coming from optional navigation doesn't work on InMemory #12295

Closed

Description

@maumar

query using gears of war model:

ctx.Weapons.Select(w => w.SynergyWith).OrderBy(g => g.IsAutomatic)

query model:

from Weapon w in DbSet<Weapon>
join Weapon w.SynergyWith in DbSet<Weapon>
on Property([w], "SynergyWithId") equals (Nullable<int>)Property([w.SynergyWith], "Id") into w.SynergyWith_group
from Weapon w.SynergyWith in 
    (from Weapon w.SynergyWith_groupItem in [w.SynergyWith_group]
    select [w.SynergyWith_groupItem]).DefaultIfEmpty()
order by (bool)?[w.SynergyWith] | ?[w.SynergyWith] | [w.SynergyWith]?.IsAutomatic == True? == True? asc
select [w.SynergyWith]

query plan:

(QueryContext queryContext) => IEnumerable<Weapon> _InterceptExceptions(
|__ source: IEnumerable<Weapon> _Select(
|   |__ source: IOrderedEnumerable<TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>>, Weapon>> _OrderBy(
|   |   |__ source: IEnumerable<TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>>, Weapon>> _SelectMany(
|   |   |   |__ source: IEnumerable<TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>>> _GroupJoin(
|   |   |   |   |__ outer: IEnumerable<ValueBuffer> ProjectionQuery(
|   |   |   |   |   |__ queryContext: queryContext, 
|   |   |   |   |   |__ entityType: EntityType: Weapon), 
|   |   |   |   |__ inner: IEnumerable<Weapon> EntityQuery(
|   |   |   |   |   |__ queryContext: queryContext, 
|   |   |   |   |   |__ entityType: EntityType: Weapon, 
|   |   |   |   |   |__ key: Key: Weapon.Id PK, 
|   |   |   |   |   |__ materializer: (IEntityType entityType | MaterializationContext materializationContext) => 
|   |   |   |   |   |   {
|   |   |   |   |   |       instance = new Weapon()
|   |   |   |   |   |       instance.<Id>k__BackingField = int TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 0, Weapon.Id)
|   |   |   |   |   |       instance.<AmmunitionType>k__BackingField = Nullable<AmmunitionType> TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 1, Weapon.AmmunitionType)
|   |   |   |   |   |       instance.<IsAutomatic>k__BackingField = bool TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 2, Weapon.IsAutomatic)
|   |   |   |   |   |       instance.<Name>k__BackingField = string TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 3, Weapon.Name)
|   |   |   |   |   |       instance.<OwnerFullName>k__BackingField = string TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 4, Weapon.OwnerFullName)
|   |   |   |   |   |       instance.<SynergyWithId>k__BackingField = Nullable<int> TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 5, Weapon.SynergyWithId)
|   |   |   |   |   |       return instance
|   |   |   |   |   |   }, 
|   |   |   |   |   |__ queryStateManager: False), 
|   |   |   |   |__ outerKeySelector: (ValueBuffer w) => Nullable<int> TryReadValue(w, 5, Weapon.SynergyWithId), 
|   |   |   |   |__ innerKeySelector: (Weapon w.SynergyWith) => (Nullable<int>)int GetValueFromEntity(
|   |   |   |   |       clrPropertyGetter: ClrPropertyGetter<Weapon, int>, 
|   |   |   |   |       entity: w.SynergyWith), 
|   |   |   |   |__ resultSelector: (ValueBuffer w | IEnumerable<Weapon> w.SynergyWith_group) => TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>> CreateTransparentIdentifier(
|   |   |   |       |__ outer: w, 
|   |   |   |       |__ inner: w.SynergyWith_group)), 
|   |   |   |__ collectionSelector: (TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>> t0) => IEnumerable<Weapon> DefaultIfEmpty(t0.Inner), 
|   |   |   |__ resultSelector: (TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>> t0 | Weapon w.SynergyWith) => TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>>, Weapon> CreateTransparentIdentifier(
|   |   |       |__ outer: t0, 
|   |   |       |__ inner: w.SynergyWith)), 
|   |   |__ expression: (TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>>, Weapon> t1) => (bool)?t1.Inner | ?t1.Inner | t1.Inner?.IsAutomatic == True? == True?, 
|   |   |__ orderingDirection: Asc), 
|   |__ selector: (TransparentIdentifier<TransparentIdentifier<ValueBuffer, IEnumerable<Weapon>>, Weapon> t1) => t1.Inner), 
|__ contextType: TestModels.GearsOfWarModel.GearsOfWarContext, 
|__ logger: DiagnosticsLogger<Query>, 
|__ queryContext: queryContext)

exception:

Nullable object must have a value.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at lambda_method(Closure , TransparentIdentifier2 ) at System.Linq.EnumerableSorter2.ComputeKeys(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter1.Sort(TElement[] elements, Int32 count) at System.Linq.OrderedEnumerable1.d__1.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext()
at System.Linq.Buffer1..ctor(IEnumerable1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at Microsoft.EntityFrameworkCore.TestUtilities.QueryAsserter1.d__181.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.EntityFrameworkCore.Query.QueryTestBase1.AssertQuery[TItem1](Func2 actualQuery, Func2 expectedQuery, Func2 elementSorter, Action2 elementAsserter, Boolean assertOrder, Int32 entryCount)
at Microsoft.EntityFrameworkCore.Query.GearsOfWarQueryTestBase`1.Double_order_by_on_nullable_bool_coming_from_optional_navigation()
at Microsoft.EntityFrameworkCore.Query.GearsOfWarQueryInMemoryTest.Double_order_by_on_nullable_bool_coming_from_optional_navigation()

Activity

  1. maumar commented on Jun 7, 2018

    @maumar
    ContributorAuthor

    problem here is that in order by clause we are converting bool? ordering back to bool to match the type, which can break if the ordering resolves to null. We could convert to ordering == true instead, but need to investigate if that doesn't have side-effects for other scenarios (different data returned)

    NavigationRewritingExpressionVisitor -> CompensateForNullabilityDifference

                if (needsTypeCompensation && originalType.UnwrapNullableType() == typeof(bool))
                {
                    return Expression.Equal(expression, Expression.Constant(true, expression.Type));
                }
    
    
  2. added this to the milestone on Jun 8, 2018
  3. modified the milestones: , 3.0.0 on Aug 3, 2018
  4. modified the milestones: 3.0.0, Backlog on Jun 20, 2019
  5. modified the milestones: Backlog, 3.1.0 on Dec 6, 2019
  6. smitpatel commented on Dec 6, 2019

    @smitpatel
    Contributor

    Working correctly in 3.1

  7. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions