2

My application is in .net core pointing to 1.1.1 framework. DB provider which I am using is Pomelo.EntityFrameworkCore.MySql

The query which I am trying to execute is as follows:

var queryEF = (from b in context.table1
                                orderby b.column1 descending
                                select new Table1
                                {
                                    column1 = b.column1,
                                });

queryEF = queryEF.Skip(3).Take(10).AsQueryable();

var query2 = (from b in queryEF
              join lb in context.table2
              on b.column1 equals lb.column1
              into tablejoin
              from blb in 
              tablejoin.DefaultIfEmpty()
              select new
              {
                 b.column1
              });

              if (query2 != null)
              {
                 foreach (var locnBranch in query2.AsEnumerable())
                 {
                 }
              }

The above query fails, at execution of AsEnumerable(). It throws 'Must be reducible node' error.

Exception details:

{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.RewriteMemberExpression(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](Expression`1 lambda)
   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
   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](Expression`1 lambda)
   at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at System.Linq.Expressions.Expression`1.Compile(Boolean preferInterpretation)
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]()
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, ILogger logger, Type contextType)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0`1.<CompileQuery>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)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at GetAll() in E:\Test\Repository.cs:line 291
   at Get(List`1 Keys, String operatingCompany, Boolean isTrading, Int32 firstRecord, Int32 count, String clientIPAddress, String sortBy, String sortOrder) in E:\Test\Repository.cs:line 51
   at Get(List`1 Keys, String operatingCompany, Boolean isTrading, Int32 firstRecord, Int32 count, String clientIPAddress, String sortBy, String sortOrder) in E:\BusinessServices\Service.cs:line 121
   at Get(List`1 Keys, String operatingCompany, Boolean isTrading, Int32 firstRecord, Int32 count, String clientIPAddress, String sortBy, String sortOrder) in E:\Controllers\esController.cs:line 54
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()}

The above query works fine, after removing skip/take from it. I tested the query in EF5 and it works without any issue, its failing only in EF core.

1
  • 1
    Yeah, this is one of the many current EF Core bugs. Changing queryEF.Skip(3).Take(10).AsQueryable() to queryEF.Skip(3).Take(10).Select(x => x) fixes the exception (at least in 1.1.2), but the join is performed in memory rather that with SQL query.
    – Ivan Stoev
    Jun 22, 2017 at 19:01

2 Answers 2

2

I could solve this by adding .AsNoTracking() at the end of queryEF query.

0

Have you tried sending your queryEF to a List instead? Also, what is the reason for taking queryEf and then running a second line of code to skip(3).Take(10)?

var queryEF = (from b in context.table1
                            orderby b.column1 descending
                            select new Table1
                            {
                                column1 = b.column1,
                            }).Skip(3).Take(10).ToList();

Instead of

queryEF = queryEF.Skip(3).Take(10).ToList();

If that does not work then instead of making the foreach loop attempt to return an IEnumerable, Change that to be specified at list using .ToList() as well.

if (queryEF != null)
          {
             foreach (var locnBranch in queryEF.ToList())
             {
             }
          }

I believe the problem your running into is the foreach statement needs a node to begin executing on an the current return you are providing is a generic which will not contain an Enumerable object to iterate through. By passing it as a List instead, you are providing those nodes the iteration needs.

Good luck

I don't have enough reputation to Comment Sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy