System.Linq.Expressions - 由于对象的当前状态,操作无效

时间:2016-08-29 22:37:20

标签: c# asp.net-mvc linq asp.net-core

在我的ASP.NET Core MVC应用程序中,我在跟随引用其他LINQ查询的LINQ查询时遇到上述错误。为了简化这篇文章,为了简洁起见,我在这里修改了代码,因为实际的查询太长而且复杂。 问题:可能导致错误的原因是什么?我应该在哪里进一步调试问题?如果有帮助,错误详情如下所示。

视图模型

public class MyViewModel
{
    public string Prop1 { get; set; }
    public int Prop2 { get; set; }
}

控制器

public ActionResult Index()
{
    var vm = (from q5 in LINQ_Qry5
             join q4 in LINQ_Qry4 on q5.SomeID equals q4.SomeID
             select new MyViewModel { Prop1= q5.property1, Prop2 = q4.property2}).ToList();
            return View(vm);
}

错误详情

  Message=Operation is not valid due to the current state of the object.
  Source=System.Linq.Expressions
  StackTrace:
       at System.Linq.Expressions.MethodCallExpression2.GetArgument(Int32 index)
       at System.Dynamic.Utils.ListArgumentProvider.get_Item(Int32 index)
       at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.QueryFlattener.Flatten(MethodCallExpression methodCallExpression)
       at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.OptimizeJoinClause(JoinClause joinClause, QueryModel queryModel, Int32 index, Action baseVisitAction, MethodInfo operatorToFlatten, Boolean outerJoin)
       at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitJoinClause(JoinClause joinClause, QueryModel queryModel, Int32 index)
       at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
       at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitSubQuery(SubQueryExpression expression)
       at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression node)
       at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection)
       at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
       at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
       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()

2 个答案:

答案 0 :(得分:1)

我打算将此作为评论,但它说的人物太多了。

无论如何,从我的谷歌搜索此错误,似乎问题在于您的变量中的任何一个(或某些) null 为空

确保以下几点:

  1. 您的join不会返回null或空序列
  2. 您的property1property2 q5q4存在且不为空
  3. 当vm为空或为空时,也许还要确保View(vm)不是问题。
  4. 我检查了一些链接:

    UpdateException: Operation is not valid due to...

    MonoTouch & LINQ - Operation is not valid due to the current state of the object

答案 1 :(得分:0)

var vm = (from q5 in LINQ_Qry5
             join q4 in LINQ_Qry4 on q5.SomeID equals q4.SomeID
             select new MyViewModel() { Prop1= q5.property1, prop2 = q4.property2}).ToList();
            return View(vm);