Search code examples
c#.netlinqentity-framework-core.net-core-3.1

InvalidOperationException: An exception was thrown while attempting to evaluate a LINQ query parameter expression


I have a problem, in the source code, a query LINQ. After upgrading to .net core 3.1 from 2.2.

    public IQueryable<Data.Model.Content> GetAll()
    {
        return dbContext.Content.Include(a => a.ContentTemplate);
    }

    public bool ExistsSlug(int id, string name)
    {
        return GetAll()
            .Any(x => x.Name.ToLower() == name.ToLower() && x.Id != id);
    }

Error :

NullReferenceException: Object reference not set to an instance of an object.

lambda_method(Closure )

InvalidOperationException: An exception was thrown while attempting to evaluate a LINQ query parameter expression. To show additional information call EnableSensitiveDataLogging() when overriding DbContext.OnConfiguring.

Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.GetValue(Expression expression, out string parameterName)

    return GetAll().Any(x => x.Name.ToLower() == name.ToLower() && x.Id != id);

System.Linq.Queryable.Any<TSource>(IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)

lambda_method(Closure , object , object[] )

Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)

Solution

  • I have created the exact scenario you are using, with .net core 3.1.

    the only test case that generated the same error was when i sent the (NAME STRING NULL) the error i got

    you can bypass this issue by adding conditional statement in your query like this:

    GetAll().Any(x=> (string.IsNullOrEmpty(name) || x.Name.ToLower() == name.ToLower()) && x.Id  != id);