.NET Core 3.1.2. Any() error "When called from 'VisitLambda', rewriting a node of type 'System.Linq.Expressions.ParameterExpression"

Viewed 425

"InvalidOperationException: When called from 'VisitLambda', rewriting a node of type 'System.Linq.Expressions.ParameterExpression' must return a non-null value of the same type. Alternatively, override 'VisitLambda' and change it to not visit children of this type."

I got this error when use Any(). Without Any(), no error.

@model List<WebApplication10.Models.UserCourse>
@{
     var db = new WebApplication10.Models.TestContext();
     var list = db.Course.Select(x => new SelectListItem()
     {
         Value = x.Code,
         Text = x.Name,
ERROR -> Selected = Model.Any(y => y.Course == x.Code)
     }).ToList();

     if (list != null && list.Any())
     {
         for (int i = 0; i <= list.Count() - 1; i++)
         {
              <input type="hidden" asp-for="@list[i].Value" />
              <input type="hidden" asp-for="@list[i].Text" />
              <input type="checkbox" asp-for="@list[i].Selected" /> @list[i].Text
              <br />
         }
     }
}

This one used to work fine in old .net core 2.2. But new .net core 3.1.2 got error. If I do Any() checking inside for loop, it works fine. How to solve Any() in Select() linq?

0 Answers
Related