{"action":"closed","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3811","repository_url":"https://api.github.com/repos/aspnet/EntityFramework","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3811/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3811/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3811/events","html_url":"https://github.com/aspnet/EntityFramework/issues/3811","id":117931294,"number":3811,"title":".OrderBy on column 2 or more navigation properties away + .Include throws exception","user":{"login":"jparish9","id":3588675,"avatar_url":"https://avatars.githubusercontent.com/u/3588675?v=3","gravatar_id":"","url":"https://api.github.com/users/jparish9","html_url":"https://github.com/jparish9","followers_url":"https://api.github.com/users/jparish9/followers","following_url":"https://api.github.com/users/jparish9/following{/other_user}","gists_url":"https://api.github.com/users/jparish9/gists{/gist_id}","starred_url":"https://api.github.com/users/jparish9/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jparish9/subscriptions","organizations_url":"https://api.github.com/users/jparish9/orgs","repos_url":"https://api.github.com/users/jparish9/repos","events_url":"https://api.github.com/users/jparish9/events{/privacy}","received_events_url":"https://api.github.com/users/jparish9/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/EntityFramework/labels/bug","name":"bug","color":"f7c6c7"},{"url":"https://api.github.com/repos/aspnet/EntityFramework/labels/pri0","name":"pri0","color":"e11d21"}],"state":"closed","locked":false,"assignee":{"login":"maumar","id":1591930,"avatar_url":"https://avatars.githubusercontent.com/u/1591930?v=3","gravatar_id":"","url":"https://api.github.com/users/maumar","html_url":"https://github.com/maumar","followers_url":"https://api.github.com/users/maumar/followers","following_url":"https://api.github.com/users/maumar/following{/other_user}","gists_url":"https://api.github.com/users/maumar/gists{/gist_id}","starred_url":"https://api.github.com/users/maumar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maumar/subscriptions","organizations_url":"https://api.github.com/users/maumar/orgs","repos_url":"https://api.github.com/users/maumar/repos","events_url":"https://api.github.com/users/maumar/events{/privacy}","received_events_url":"https://api.github.com/users/maumar/received_events","type":"User","site_admin":false},"milestone":null,"comments":32,"created_at":"2015-11-19T23:19:05Z","updated_at":"2016-05-04T21:49:54Z","closed_at":"2016-05-04T21:49:54Z","body":"Windows 10, VS 2015, IIS Express, SQL Server Express 2014, EF 7.0.0-rc1-final, ASP.NET 6.0.0-rc1-final\r\n\r\nAfter upgrading to RC1, I am seeing an exception `Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.ParameterExpression'.` when querying using `.OrderBy` on a navigation property table 2 or more tables away from the base table, plus `.Include`.  This was working in beta8.  I reduced the test case as much as possible to illustrate:\r\n\r\nModels:\r\n```c#\r\n    public class Blog\r\n    {\r\n        public int BlogId { get; set; }\r\n\r\n        public string Title { get; set; }\r\n    }\r\n```\r\n\r\n```c#\r\n    public class Post\r\n    {\r\n        public int PostId { get; set; }\r\n\r\n        public int BlogId { get; set; }\r\n\r\n        public Blog Blog { get; set; }\r\n\r\n        public string PostText { get; set; }\r\n    }\r\n```\r\n\r\n```c#\r\n    public class Comment\r\n    {\r\n        public int CommentId { get; set; }\r\n\r\n        public int PostId { get; set; }\r\n\r\n        public Post Post { get; set; }\r\n\r\n        public int CommentText { get; set; }\r\n    }\r\n```\r\n\r\nThe query that causes an exception is:\r\n```c#\r\n   _dbContext.Comments\r\n                    .Include(c => c.Post)\r\n                    .OrderBy(c => c.Post.Blog.BlogId)\r\n                    .ToList();\r\n```\r\n\r\nStack trace:\r\n```\r\nUnable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.ParameterExpression'.\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.VisitMethodCall(MethodCallExpression expression)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.VisitMethodCall(MethodCallExpression expression)\r\n   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n   at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)\r\n   at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.IncludeNavigations(IncludeSpecification includeSpecification, Type resultType, LambdaExpression accessorLambda, Boolean querySourceRequiresTracking)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel, IReadOnlyCollection`1 includeSpecifications)\r\n   at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.IncludeNavigations(QueryModel queryModel, IReadOnlyCollection`1 includeSpecifications)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)\r\n   at Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel queryModel)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_0`1.<CompileQuery>b__0()\r\n   at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query)\r\n   at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n   at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n   at Remotion.Linq.QueryableBase`1.GetEnumerator()\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...\r\n```\r\n\r\nBoth the `.Include` and the `.OrderBy` are necessary to see this exception, and the `.OrderBy` must be ordering by a column at least 2 navigation properties away from the base table.  For example,\r\n\r\n```c#\r\n    _dbContext.Comments\r\n                    .Include(c => c.Post)\r\n                    .OrderBy(c => c.Post.BlogId)\r\n                    .ToList();\r\n```\r\n\r\nand\r\n```c#\r\n   _dbContext.Comments\r\n                    .OrderBy(c => c.Post.Blog.BlogId)\r\n                    .ToList();\r\n```\r\nboth work fine.\r\n\r\n"}}