{"action":"reopened","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/6591","repository_url":"https://api.github.com/repos/aspnet/EntityFramework","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/6591/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/6591/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/6591/events","html_url":"https://github.com/aspnet/EntityFramework/issues/6591","id":178684971,"number":6591,"title":"ThenInclude and conditional orderby not working together","user":{"login":"annayafi","id":8353486,"avatar_url":"https://avatars.githubusercontent.com/u/8353486?v=3","gravatar_id":"","url":"https://api.github.com/users/annayafi","html_url":"https://github.com/annayafi","followers_url":"https://api.github.com/users/annayafi/followers","following_url":"https://api.github.com/users/annayafi/following{/other_user}","gists_url":"https://api.github.com/users/annayafi/gists{/gist_id}","starred_url":"https://api.github.com/users/annayafi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/annayafi/subscriptions","organizations_url":"https://api.github.com/users/annayafi/orgs","repos_url":"https://api.github.com/users/annayafi/repos","events_url":"https://api.github.com/users/annayafi/events{/privacy}","received_events_url":"https://api.github.com/users/annayafi/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-09-22T18:26:34Z","updated_at":"2016-09-23T19:42:07Z","closed_at":null,"body":"### Steps to reproduce\r\nQuite a simple query with a nested include though:\r\n```\r\n  var test = (from post in dbContext.Posts\r\n                     orderby (post.Language == \"fr\") ? 1 : 2\r\n                     select post).Include(p => p.PostTags).ThenInclude(pt => pt.Tag);\r\n            var testResult = test.ToList();\r\n```\r\nKnowing that a post has a collection of posttags which themselves own a tag.\r\n\r\n\r\n ### The issue\r\nThe query can't be materialized because generated sql is wrong:\r\n\r\nIf you are seeing an exception, include the full exceptions details (message and stack trace).\r\n\r\n```\r\nException message:\r\nAdditional information: No column name was specified for column 1 of 'post0'.\r\nThe multi-part identifier \"post.Language\" could not be bound.\r\nStack trace:\r\n{System.Data.SqlClient.SqlException: No column name was specified for column 1 of 'post0'.\r\nThe multi-part identifier \"post.Language\" could not be bound.\r\n   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at System.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)\r\n   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)\r\n   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)\r\n   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues, Boolean manageConnection)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCollectionIterator.<GetRelatedValues>d__4.MoveNext()\r\n   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()\r\n   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.Include(QueryContext queryContext, Object entity, IReadOnlyList`1 navigationPath, IReadOnlyList`1 relatedEntitiesLoaders, Int32 currentNavigationIndex, Boolean queryStateManager)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.Include(QueryContext queryContext, Object entity, IReadOnlyList`1 navigationPath, IReadOnlyList`1 relatedEntitiesLoaders, Boolean queryStateManager)\r\n   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__30`1.MoveNext()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n[ MyProject.ToList()]\r\n```\r\n\r\n**Generated sql:**\r\n````t-sql\r\nSELECT [post].[Id], [post].[Content], [post].[CreationDate], [post].[Language], [post].[Rating], [post].[UpdateDate], [post].[UserId]\r\nFROM [Posts] AS [post]\r\nORDER BY CASE\r\n    WHEN [post].[Language] = N'fr'\r\n    THEN 1 ELSE 2\r\nEND, [post].[Id]\r\nMicrosoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']\r\nSELECT [p].[Id], [p].[PostId], [p].[TagId], [p].[Words], [t].[Id], [t].[Key], [t].[Name], [t].[ParentTagId]\r\nFROM [PostTags] AS [p]\r\nINNER JOIN (\r\n    SELECT DISTINCT CASE\r\n        WHEN [post].[Language] = N'fr'\r\n        THEN 1 ELSE 2\r\n    END, [post].[Id]\r\n    FROM [Posts] AS [post]\r\n) AS [post0] ON [p].[PostId] = [post0].[Id]\r\nINNER JOIN [Tags] AS [t] ON [p].[TagId] = [t].[Id]\r\nORDER BY CASE\r\n    WHEN [post].[Language] = N'fr'\r\n    THEN 1 ELSE 2\r\nEND, [post0].[Id]\r\n````\r\n>> you can see at the end of the query, there's still post identifier whereas this should be \"post0\"\r\n\r\nNevertheless, it works as expected with a normal order by, without the condition inside I mean. \r\n\r\n\r\n### Further technical details\r\n\r\nEF Core version: (found in project.json or packages.config) 1.0.0 or 1.0.1\r\nOperating system: Win 8 \r\nVisual Studio version: (e.g. VS 2013 or n/a) vs 2015\r\n"}}
{"actor":{"display_login":"smitpatel"}}