Skip to content

Expressions without explicit type causes filter to be eliminated/false #35059

@cincuranet

Description

@cincuranet
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;

using var db = new MyContext();
var parameter = Expression.Parameter(typeof(Havit), "o");
var equal = Expression.Equal(Expression.Property(parameter, "Deleted"), Expression.Constant(null));
var equal2 = Expression.Equal(Expression.Property(parameter, "Deleted"), Expression.Constant(null, typeof(DateTime?)));
var expr = Expression.Lambda<Func<Havit, bool>>(equal, parameter);
var query = db.Set<Havit>().Where(x => x.Deleted == null).Where(x => x.Deleted == null).ToQueryString();
var query2 = db.Set<Havit>().Where(expr).Where(x => x.Deleted == null).ToQueryString();
Console.WriteLine(query);
Console.WriteLine();
Console.WriteLine(query2);

class MyContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Havit>();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite();
    }
}

class Havit
{
    public int Id { get; set; }
    public DateTime? Deleted { get; set; }
}

The first query properly results in this SQL.

SELECT "h"."Id", "h"."Deleted"
FROM "Havit" AS "h"
WHERE "h"."Deleted" IS NULL

While the second results in this unexpected SQL.

SELECT "h"."Id", "h"."Deleted"
FROM "Havit" AS "h"
WHERE 0

The culprit seems to be that the expression var equal = Expression.Equal(Expression.Property(parameter, "Deleted"), Expression.Constant(null)); does not contain the type information like the var equal2 = Expression.Equal(Expression.Property(parameter, "Deleted"), Expression.Constant(null, typeof(DateTime?))); which works fine.

Include provider and version information

EF Core version: 8.0.10 and 9.0 RC2
Database provider: unrelated
Target framework: .NET 8.0

cc @hakenr

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions