1

In my ASP.NET Core MVC app I'm getting the above error in following LINQ Query that reference other LINQ Queries. For the sake of simplicity of this post, I've modified the code here for brevity since the actual queries are too long and complex. Question: What could be possible causes of the error and where should I look to further debug the issue? The error details are shown below if that can help.

ViewModel:

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

Controller:

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);
}

Error Details:

  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()
nam
  • 21,967
  • 37
  • 158
  • 332
  • This is probably because of the big amount of control being generated to model for your view to process. http://stackoverflow.com/questions/8832470/operation-is-not-valid-due-to-the-current-state-of-the-object-error-during-pos – Orel Eraki Aug 29 '16 at 22:41
  • Not sure if its necessary, but your `join` doesn't have a following `on` clause. – Pat Aug 29 '16 at 22:44
  • @Pat That was just a typo. Thanks for pointing out - I corrected it. But the error is there still. – nam Aug 29 '16 at 22:47
  • EF Core? That's the answer. No comment. Go to their bug tracking place. – Ivan Stoev Aug 29 '16 at 22:51
  • Another small and insignificant typo, `Prop1` and `Prop2` should be `property1` and `property2` – Keyur PATEL Aug 30 '16 at 02:33
  • @KeyurPATEL Thank you. I corrected it in the MyViewModel. – nam Aug 30 '16 at 15:03
  • Did you figure this out? I'm having this problem when including an entity and performing a where clausule on a property of the included entity. – Cyril Mestrom Oct 28 '16 at 10:38

2 Answers2

2

I was going to put this as a comment but it said too many characters.

Anyways, from my google searches on this error, it would seem the problem lies in any one (or some) of your variables being null or empty.

Make sure of a few things:

  1. Your join does not return null or empty sequence
  2. Your property1 and property2 of q5 and q4 exist and are not null
  3. Maybe also make sure View(vm) isn't the problem when vm is empty or null.

Some links I checked out:

UpdateException: Operation is not valid due to...

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

Community
  • 1
  • 1
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
  • Thank you for sharing your thoughts. q5 and q4 are the LINQ queries that are referencing other LINQ queries that in turn are referencing other LINQ queries. Let me check if something somewhere is resulting in a null. Thanks also for providing the relevant links as these links also shed some light on possible causes of the error. – nam Aug 31 '16 at 02:50
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);
DBB
  • 137
  • 1
  • 4