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 parameter in Select with ?: conditional operator throws InvalidOperationException #17942

Closed
tibitoth opened this issue Sep 19, 2019 · 4 comments · Fixed by #18640
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@tibitoth
Copy link

tibitoth commented Sep 19, 2019

Description

If you use a nullable parameter in a select expression with ?: conditional operator, the EFCore provider evaulates the nullable object's value even that condition branch should be skipped.

Steps to reproduce

Consider this entity and query,

public class TestEntity
{
    public int Id { get; set; }
    public DateTime TestDateTime { get; set; }
}

public class TestDto
{
    public bool Result { get; set; }
}

public class IndexModel : PageModel
{
    private readonly ApplicationDbContext _context;

    public IndexModel(ApplicationDbContext context)
    {
        _context = context;
    }

    public async Task OnGet()
    {
        var r1 = await ReproMethodAsync(DateTime.Now);
        var r2 = await ReproMethodAsync(null); // InvalidOperationException: Nullable object must have a value.
    }

    private async Task<List<TestDto>> ReproMethodAsync(DateTime? param)
    {
        var results = await _context.Tests.Select(x => new TestDto
        {
            Result = param != null ? x.TestDateTime == param.Value : true,
        }).ToListAsync();

        return results;
    }
}

StackTrace

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)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Evaluate(Expression expression, bool generateParameter)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression)
System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)
System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression)
System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)
System.Linq.Expressions.ConditionalExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression)
System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
System.Linq.Expressions.ExpressionVisitor.Visit<T>(ReadOnlyCollection<T> nodes, Func<T, T> elementVisitor)
System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression)
System.Linq.Expressions.ExpressionVisitor.VisitLambda<T>(Expression<T> node)
System.Linq.Expressions.Expression<TDelegate>.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression)
System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression)
System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.Visit(Expression expression)
Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.ExtractParameters(Expression expression)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExtractParameters(Expression query, IParameterValues parameterValues, IDiagnosticsLogger<Query> logger, bool parameterize, bool generateContextAccessors)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<TResult>.GetAsyncEnumerator(CancellationToken cancellationToken)
System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable<T>.GetAsyncEnumerator()
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)

Further technical details

EF Core version: 2.2, 3.0-rc1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET FW 4.x with 2.2, .NET Core 3.0-rc1
Operating system: WIndows 10 10.0.18362.356
IDE: Visual Studio 2019 16.3.0-preview4

@tibitoth tibitoth changed the title Nullable parameter in Select with ?: conditional operator throws NRE Nullable parameter in Select with ?: conditional operator throws InvalidOperationException Sep 19, 2019
@smitpatel smitpatel added this to the 3.1.0 milestone Sep 20, 2019
@smitpatel
Copy link
Contributor

Related #13859

@roji
Copy link
Member

roji commented Oct 29, 2019

Will take a look at this.

Note: related to #12521 which was fixed for 2.2.0.

@roji roji assigned roji and unassigned maumar Oct 29, 2019
@roji roji added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed poachable labels Oct 29, 2019
@fschlaef
Copy link

fschlaef commented Nov 6, 2019

@smitpatel @roji Confirmed fixed as of 3.1.0-preview3.19554.8 👍

@roji
Copy link
Member

roji commented Nov 6, 2019

@fschlaef thanks for confirming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants