{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/11425","repository_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore","labels_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/11425/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/11425/comments","events_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/11425/events","html_url":"https://github.com/aspnet/EntityFrameworkCore/issues/11425","id":308368099,"number":11425,"title":"`Must be reducible node` when casting DateTime to nullable DateTime","user":{"login":"QuantumHive","id":2554952,"avatar_url":"https://avatars3.githubusercontent.com/u/2554952?v=4","gravatar_id":"","url":"https://api.github.com/users/QuantumHive","html_url":"https://github.com/QuantumHive","followers_url":"https://api.github.com/users/QuantumHive/followers","following_url":"https://api.github.com/users/QuantumHive/following{/other_user}","gists_url":"https://api.github.com/users/QuantumHive/gists{/gist_id}","starred_url":"https://api.github.com/users/QuantumHive/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QuantumHive/subscriptions","organizations_url":"https://api.github.com/users/QuantumHive/orgs","repos_url":"https://api.github.com/users/QuantumHive/repos","events_url":"https://api.github.com/users/QuantumHive/events{/privacy}","received_events_url":"https://api.github.com/users/QuantumHive/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-25T17:24:15Z","updated_at":"2018-03-26T11:21:53Z","closed_at":null,"author_association":"NONE","body":"I'd like to cast a `DateTime` property from my entity model to a nullable `DateTime` and set it on my [DTO](https://en.wikipedia.org/wiki/Data_transfer_object). Whenever I do that in my select statement within my linq query I'm getting the following exception:\r\n\r\n```\r\nException message: must be reducible node\r\nStack trace:\r\n\r\nSystem.ArgumentException: must be reducible node\r\n   at System.Linq.Expressions.Expression.ReduceAndCheck()\r\n   at System.Linq.Expressions.Expression.ReduceExtensions()\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteBinaryExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda)\r\n   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteUnaryExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteConditionalExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.MemberAssignmentRewriter..ctor(MemberAssignment binding, StackSpiller spiller, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.BindingRewriter.Create(MemberBinding binding, StackSpiller spiller, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberInitExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda)\r\n   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)\r\n   at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda)\r\n   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)\r\n   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)\r\n   at System.Linq.Expressions.Expression`1.Compile(Boolean preferInterpretation)\r\n   at System.Linq.Expressions.Expression`1.Compile()\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]()\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)\r\n   at Microsoft.EntityFrameworkCore.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.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_0`1.<Execute>b__0()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n   at Remotion.Linq.QueryableBase`1.GetEnumerator()\r\n   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)\r\n   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)\r\n   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)\r\n```\r\n\r\n### Steps to reproduce\r\nGiven the following models:\r\n\r\n```c#\r\npublic class Item\r\n{\r\n    public IEnumerable<SubItem> SubItems { get; set; }\r\n}\r\npublic class SubItem\r\n{\r\n     public DateTime Date { get; set; }\r\n}\r\n```\r\n\r\n```c#\r\nfrom item in db.Set<Item>()\r\nlet subitems =\r\n    from subitem in item.SubItems\r\n    select subitem.Date\r\nselect new\r\n{\r\n    Date = subitems.Any() ? (DateTime?)subitem.First() : null\r\n}\r\n```\r\n\r\nwhere `db` is an instance of the database context.\r\n\r\n### Further technical details\r\nEF Core version: 2.0\r\nDatabase Provider: Microsoft.EntityFrameworkCore.SqlServer\r\nOperating system: Windows 10\r\nIDE: Visual Studio 2017 15.6.4\r\n"},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/comments/376132635","html_url":"https://github.com/aspnet/EntityFrameworkCore/issues/11425#issuecomment-376132635","issue_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/11425","id":376132635,"user":{"login":"QuantumHive","id":2554952,"avatar_url":"https://avatars3.githubusercontent.com/u/2554952?v=4","gravatar_id":"","url":"https://api.github.com/users/QuantumHive","html_url":"https://github.com/QuantumHive","followers_url":"https://api.github.com/users/QuantumHive/followers","following_url":"https://api.github.com/users/QuantumHive/following{/other_user}","gists_url":"https://api.github.com/users/QuantumHive/gists{/gist_id}","starred_url":"https://api.github.com/users/QuantumHive/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QuantumHive/subscriptions","organizations_url":"https://api.github.com/users/QuantumHive/orgs","repos_url":"https://api.github.com/users/QuantumHive/repos","events_url":"https://api.github.com/users/QuantumHive/events{/privacy}","received_events_url":"https://api.github.com/users/QuantumHive/received_events","type":"User","site_admin":false},"created_at":"2018-03-26T11:21:53Z","updated_at":"2018-03-26T11:21:53Z","author_association":"NONE","body":"After some more digging and hours of headaches, I've noticed the exception is thrown because of the `.Any()`  call. Casting has nothing to do with it. Let's simplify the example:\r\n\r\n```\r\nfrom item in db.Set<Item>()\r\nlet subitems =\r\n    from subitem in item.SubItems\r\n    where subitem.Date < someDate\r\n    select subitem.Date\r\nwhere subItems.Any()\r\nselect new\r\n{\r\n    Count = subitems.Count()\r\n}\r\n```\r\n\r\nIt appears that whenever I put the `where subItems.Any()`, the exception gets thrown. If I remove that line, then no exception is thrown and the query performs (which would make the query invalid).  \r\nHowever my workaround is:\r\n\r\n```\r\nfrom item in db.Set<Item>()\r\nlet subitems = (\r\n    from subitem in item.SubItems\r\n    where subitem.Date < someDate\r\n    select subitem.Date).ToArray()\r\nwhere subItems.Any()\r\nselect new\r\n{\r\n    Count = subitems.Count()\r\n}\r\n```\r\n\r\nto put a `.ToArray()` around the subquery. However this really has a big impact on performance, so this is not a workable solution/workaround. In production, this query alone would already hit more than 10 seconds, and put the 20 DTU's on the Azure SQL Database at 100%, which is obviously unacceptable.  \r\n\r\nWhy is this exception thrown? Is this a confirmed bug? Or am I doing something wrong?  \r\n\r\nAlso I'd like to suggest to improve the exception message. The message doesn't help me to find the root of the problem. My example was just an extremely simplified version of the query I'm dealing with in code."}}
{"actor":{"display_login":"QuantumHive"}}