Skip to content

Nullable<Guid>.GetValueOrDefault is not translated #22243

@NRKirby

Description

@NRKirby

I get an InvalidOperationException when I try filtering a DbSet on a property that is a nullable Guid.

Steps to reproduce

When I execute the following query on an Orders DbSet where MachineId is a nullable Guid.

var machineIds // a list of Guids

var orders = _context.Orders.Where(x => machineIds.Contains(x.MachineId.GetValueOrDefault()));

I get the following exception

Message: 
    System.InvalidOperationException : The LINQ expression 'DbSet<Order>
        .Where(m => __machineIds_0
            .Contains((Nullable<Guid>)m.MachineId.GetValueOrDefault()))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
  Stack Trace: 
    QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& )
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    MethodCallExpression.Accept(ExpressionVisitor visitor)
    QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
    Database.CompileQuery[TResult](Expression query, Boolean async)
    QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
    <>c__DisplayClass9_0`1.<Execute>b__0()
    CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
    CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
    QueryCompiler.Execute[TResult](Expression query)
    EntityQueryProvider.Execute[TResult](Expression expression)
    Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
    QueryHandler.GetProducts(Query request, CancellationToken cancellationToken, String normalisedCultureCode, IList`1 allVideos, IList`1 allImages, IList`1 allBrochures, IList`1 allSpecs, IList`1 allMachines) line 385
    QueryHandler.Handle(Query request, CancellationToken cancellationToken) line 127
    RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
    RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
    TestContext.ExecuteScopeAsync[T](Func`2 action) line 156
    GetByHubShould.ReturnCorrectFeaturedSpecifications() line 372
    <>c.<ThrowAsync>b__139_0(Object state)

-->

Further technical details

EF Core version: 3.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.1
Operating system: Windows
IDE: Visual Studio 2019 16.7.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions