Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: System.ArgumentException: must be reducible node when using ternary operator #6921

Closed
mb2140 opened this issue Nov 2, 2016 · 4 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mb2140
Copy link

mb2140 commented Nov 2, 2016

Steps to reproduce

Trying to run this query which works in EF6 (I don't believe this is a duplicate because other issues involved a "group-by" clause):

PlatformDTO dto = UnitOfWork.Projects.Find()
     .Where(x => x.ID== id).Select(x => new PlatformDTO()
                {
                    ID = x.ID,
                    User = new PlatformUserDTO()
                    {
                             Score = x.User != null ? x.User.Score : 0,
                             UserID = x.User != null ? x.User.ID : 0,
                    }.FirstOrDefault();

{System.ArgumentException: must be reducible node
at System.Linq.Expressions.Expression.ReduceAndCheck()
at System.Linq.Expressions.Expression.ReduceExtensions()
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteVariableAssignment(BinaryExpression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteAssignBinaryExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteBlockExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteConditionalExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.MemberAssignmentRewriter..ctor(MemberAssignment binding, StackSpiller spiller, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.BindingRewriter.Create(MemberBinding binding, StackSpiller spiller, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberInitExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.MemberAssignmentRewriter..ctor(MemberAssignment binding, StackSpiller spiller, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.BindingRewriter.Create(MemberBinding binding, StackSpiller spiller, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberInitExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression1 lambda) at System.Linq.Expressions.Compiler.StackSpiller.RewriteLambdaExpression(Expression expr, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions) at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions) at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions) at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node) at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions) at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression1 lambda)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
at System.Linq.Expressions.Expression1.Compile(Boolean preferInterpretation) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]() at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_01.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)

EF Core version: (found in project.json or packages.config)
Operating system:
Visual Studio version: (e.g. VS 2013 or n/a)

Other details about my project setup:

@rowanmiller rowanmiller added this to the 1.2.0 milestone Nov 2, 2016
@maumar
Copy link
Contributor

maumar commented Nov 3, 2016

@mb2140 can you also provide the model you are using? Specifically, contents of OnModelCreating method on your DbContext and the entities involved: Project, User.

@candoumbe
Copy link

@maumar
Hi,

I ran into the same issues with a somewhat "basic" query

var doctors = dbContext.Doctors.Select(x => new DoctorInfo
                        {
                            Firstname = x.Firstname,
                            Lastname = x.Lastname,
                            Id = x.UUID,
                            SpecialtyId = x.Specialty != null ? x.Specialty.UUID : (Guid?)null,
                            UUID = x.UUID,
                            UpdatedDate = x.UpdatedDate
                        })

which throws the exception @mb2140 described.

The workaround I found for now is to use no ternary condition in the select clause

var doctors = dbContext.Doctors.Select(x => new DoctorInfo
                        {
                            Firstname = x.Firstname,
                            Lastname = x.Lastname,
                            Id = x.UUID,
                            //SpecialtyId = x.Specialty != null ? x.Specialty.UUID : (Guid?)null, // this is the line that causes the exception to be thrown
                            UUID = x.UUID,
                            UpdatedDate = x.UpdatedDate
                        })

I hope this will help

@maumar
Copy link
Contributor

maumar commented Apr 10, 2017

This works in the current bits and produces the following sql:

SELECT [x].[FirstName], [x].[LastName], [x].[UUID], CASE
	WHEN [x].[SpecialtyUUID] IS NOT NULL
	THEN [x.Specialty].[UUID] ELSE NULL
END, [x].[UpdatedDate]
FROM [Doctors] AS [x]
LEFT JOIN [Speciality] AS [x.Specialty] ON [x].[SpecialtyUUID] = [x.Specialty].[UUID]

@maumar maumar closed this as completed Apr 10, 2017
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 10, 2017
@ajcvickers ajcvickers changed the title EF 1.1 Preview - System.ArgumentException: must be reducible node Query: System.ArgumentException: must be reducible node when using ternary operator May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ole1986
Copy link

ole1986 commented Aug 9, 2018

I am getting the exact error message "must be reducible node" when using EF Core 2.1.1 with AspNetCore 2.1.1

The statement is pretty basic, too

var pendingArticles = ctx.ArticleTable.AsNoTracking()
                                            .Where(p => p.InheritedTable.Status <= 1)
                                            .Where(p => items.Select(p => p.ID).Contains(p.InheritedTableID))
                                            .GroupBy(p => p.ArticleID)
                                            .Select(p => new { p.Key, Qty = p.Sum(a => a.Qty) })
                                            .ToList();

UPDATED (WORKAROUND)

I finally fixed it

.Where(p => !items.Select(p => p.ID).Contains(p.InheritedTableID)); // causes the error
var Ids = items.Select(p => p.ID);
.Where(p => Ids.Contains(p.InheritedTableID)); // fixed it

So, the nested .Select(...) is the evildoer

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

7 participants