nam
nam

Reputation: 23789

System.Linq.Expressions - Operation is not valid due to the current state of the object

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

Upvotes: 1

Views: 8403

Answers (2)

Keyur PATEL
Keyur PATEL

Reputation: 2329

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

Upvotes: 2

DBB
DBB

Reputation: 137

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

Upvotes: 0

Related Questions