Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable<Guid>.GetValueOrDefault is not translated #22243

Open
NRKirby opened this issue Aug 26, 2020 · 8 comments
Open

Nullable<Guid>.GetValueOrDefault is not translated #22243

NRKirby opened this issue Aug 26, 2020 · 8 comments

Comments

@NRKirby
Copy link

NRKirby commented Aug 26, 2020

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

@smitpatel smitpatel changed the title EF Core Linq expression could not be translated Nullable<Guid>.GetValueOrDefault is not translated Aug 26, 2020
@smitpatel smitpatel added this to the Backlog milestone Aug 31, 2020
@smitpatel
Copy link
Contributor

@roji to write the work-around query.

@roji
Copy link
Member

roji commented Aug 31, 2020

Until we implement this, you can simply use the coalescing operator to work around it:

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

@roji
Copy link
Member

roji commented Dec 4, 2023

Note: consider regular COALESCE vs. IS_NULL here (see #32519, though that discussion should apply both to the regular C# coalescing operator and to GetValueOrDefault)

@John0King

This comment was marked as off-topic.

@roji

This comment was marked as off-topic.

@John0King

This comment was marked as off-topic.

@roji

This comment was marked as off-topic.

@roji

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants