1

我的应用程序位于 .net 核心中,指向 1.1.1 框架。我正在使用的数据库提供程序是 Pomelo.EntityFrameworkCore.MySql

我试图执行的查询如下:

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())
                 {
                 }
              }

在执行 AsEnumerable() 时,上述查询失败。它抛出“必须是可简化节点”错误。

异常详情:

{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()}

上面的查询在从中删除跳过/取走后工作正常。我在 EF5 中测试了查询,它没有任何问题,它只在 EF 核心中失败。

4

2 回答 2

1

我可以通过在 queryEF 查询末尾添加.AsNoTracking()来解决这个问题。

于 2017-10-01T11:43:55.017 回答
0

您是否尝试过将您的 queryEF 发送到列表?另外,获取queryEf然后运行第二行代码以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();

代替

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

如果这不起作用,那么不要让 foreach 循环尝试返回一个 IEnumerable,而是使用 .ToList() 将其更改为在列表中指定。

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

我相信您遇到的问题是 foreach 语句需要一个节点才能开始在您提供的当前返回上执行是一个泛型,它不包含要迭代的 Enumerable 对象。通过将其作为 List 传递,您将提供迭代所需的那些节点。

祝你好运

我没有足够的声望来评论对不起

于 2017-06-22T16:30:17.367 回答