Skip to content

Query: query with join, order by, include, and another order by eats up the include and produces incorrect sql #12827

@maumar

Description

@maumar

query:

                var query = ctx.Tags.Join(
                        ctx.Gears.OfType<Officer>().OrderBy(ee => ee.SquadId),
                        t => new
                        {
                            SquadId = t.GearSquadId,
                            Nickname = t.GearNickName
                        },
                        g => new
                        {
                            SquadId = (int?)g.SquadId,
                            g.Nickname
                        },
                        (t, o) => o).OrderBy(ee => ee.FullName).Include(o => o.Reports).OrderBy(oo => oo.HasSoulPatch);

                var result = query.ToList();

query model:

from Officer oo in 
    from CogTag t in DbSet<CogTag>
    join Officer o in 
        from Officer ee in DbSet<Officer>
        order by [ee].SquadId asc
        select [ee]
    on new { 
        [t].GearSquadId, 
        [t].GearNickName
     } equals new { 
        (Nullable<int>)[o].SquadId, 
        [o].Nickname
     }
    order by [o].FullName asc
    select [o]
order by [oo].HasSoulPatch asc
select [oo]

query plan:

(QueryContext queryContext) => IEnumerable<Officer> _InterceptExceptions(
|__ source: IOrderedEnumerable<Officer> _OrderBy(
|   |__ source: IEnumerable<Officer> _Select(
|   |   |__ source: IOrderedEnumerable<TransparentIdentifier<ValueBuffer, Officer>> _OrderBy(
|   |   |   |__ source: IEnumerable<TransparentIdentifier<ValueBuffer, Officer>> _Join(
|   |   |   |   |__ outer: IEnumerable<ValueBuffer> _ShapedQuery(
|   |   |   |   |   |__ queryContext: queryContext, 
|   |   |   |   |   |__ shaperCommandContext: SelectExpression: 
|   |   |   |   |   |       SELECT [t0].[GearSquadId], [t0].[GearNickName], [t0].[HasSoulPatch]
|   |   |   |   |   |       FROM [Tags] AS [t0], 
|   |   |   |   |   |__ shaper: ValueBufferShaper), 
|   |   |   |   |__ inner: IEnumerable<Officer> _ShapedQuery(
|   |   |   |   |   |__ queryContext: queryContext, 
|   |   |   |   |   |__ shaperCommandContext: SelectExpression: 
|   |   |   |   |   |       SELECT [ee2].[Nickname], [ee2].[SquadId], [ee2].[AssignedCityName], [ee2].[CityOrBirthName], [ee2].[Discriminator], [ee2].[FullName], [ee2].[HasSoulPatch], [ee2].[LeaderNickname], [ee2].[LeaderSquadId], [ee2].[Rank]
|   |   |   |   |   |       FROM [Gears] AS [ee2]
|   |   |   |   |   |       WHERE [ee2].[Discriminator] = N'Officer'
|   |   |   |   |   |       ORDER BY [ee2].[SquadId], 
|   |   |   |   |   |__ shaper: BufferedEntityShaper<Officer>), 
|   |   |   |   |__ outerKeySelector: (ValueBuffer t) => new { 
|   |   |   |   |       Nullable<int> TryReadValue(t, 0, CogTag.GearSquadId), 
|   |   |   |   |       string TryReadValue(t, 1, CogTag.GearNickName)
|   |   |   |   |__  }, 
|   |   |   |   |__ innerKeySelector: (Officer o) => new { 
|   |   |   |   |       (Nullable<int>)o.SquadId, 
|   |   |   |   |       o.Nickname
|   |   |   |   |__  }, 
|   |   |   |   |__ resultSelector: (ValueBuffer t | Officer o) => TransparentIdentifier<ValueBuffer, Officer> CreateTransparentIdentifier(
|   |   |   |       |__ outer: t, 
|   |   |   |       |__ inner: o)), 
|   |   |   |__ expression: (TransparentIdentifier<ValueBuffer, Officer> t0) => t0.Inner.FullName, 
|   |   |   |__ orderingDirection: Asc), 
|   |   |__ selector: (TransparentIdentifier<ValueBuffer, Officer> t0) => t0.Inner), 
|   |__ expression: (Officer oo) => oo.HasSoulPatch, 
|   |__ orderingDirection: Asc), 
|__ contextType: TestModels.GearsOfWarModel.GearsOfWarContext, 
|__ logger: DiagnosticsLogger<Query>, 
|__ queryContext: queryContext)

Exception:

Invalid column name 'HasSoulPatch'.
	at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
	at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
	at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
	at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
	at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
	at System.Data.SqlClient.SqlDataReader.get_MetaData()
	at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
	at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
	at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
	at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
	at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
	at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
	at System.Data.Common.DbCommand.ExecuteReader()
	at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
	at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
	at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
	at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
	at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
	at System.Linq.Enumerable.<JoinIterator>d__38`4.MoveNext()
	at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
	at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
	at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
	at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
	at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
	at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
	at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
	at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions