在我的ASP.NET Core MVC應用程序中,我在以下引用其他LINQ查詢的LINQ查詢中遇到了上述錯誤。爲了簡單起見,我在這裏簡短地修改了代碼,因爲實際的查詢太長而且很複雜。 問題:可能導致錯誤的原因是什麼以及我應該在哪裏尋找進一步調試問題?如果有幫助,錯誤的詳細信息如下所示。System.Linq.Expressions - 由於對象的當前狀態,操作無效
視圖模型:
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()
這可能是因爲控制的大量而產生的模型視圖處理。 http://stackoverflow.com/questions/8832470/operation-is-not-valid-d-to-the-current-state-of-the-object-error-during-pos –
不確定它是否有必要,但你的'join'沒有後面的'on'子句。 – Pat
@Pat這只是一個錯字。謝謝指出 - 我糾正了它。但錯誤仍然存在。 – nam