You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Negated nullable comparison in a conditional returns wrong results #39059
returns 1 for rows where NullableInt is NULL; C# gives 0. Generated SQL: CASE WHEN NOT ("e"."NullableInt" <= 1) THEN 0 ELSE 1 END. Reproduces on 10.0 and main (SQLite and SQL Server).
Cause: the nullability processor correctly wraps the comparison as CASE WHEN x THEN FALSE ELSE TRUE END, but the CASE flattening in SqlExpressionFactory.Case (#34175) turns WHEN (CASE WHEN x THEN FALSE ELSE TRUE END) THEN y into WHEN NOT x THEN y, which is NULL rather than TRUE when x is NULL.
Proposed fix: with a single WHEN clause, invert instead (CASE WHEN x THEN z ELSE y END); otherwise keep the nested CASE. I have a PR ready with a spec test.
usingMicrosoft.EntityFrameworkCore;usingSystem.Collections.Immutable;usingSystem.Linq;usingSystem;usingSystem.Linq.Expressions;usingvarcontext=newMyDbContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();Expression<Func<TestRow,int?>>expr= e =>!(e.Col1<=1)?0:1;// Through EF Coreforeach(vardbin(awaitcontext.TestRows.Select(expr).ToListAsync())){Console.WriteLine(db);}Console.WriteLine("--------------");// Through LINQforeach(vardbinTestRow.CreateTestRows().Select(expr.Compile())){Console.WriteLine(db);}publicclassMyDbContext:DbContext{publicDbSet<TestRow>TestRows=>Set<TestRow>();protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<TestRow>().Property(x =>x.Id).ValueGeneratedNever();modelBuilder.Entity<TestRow>().HasData(TestRow.CreateTestRows());}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseSqlServer($@"Server=(localdb)\mssqllocaldb;Database={GetType().Assembly.GetName().Name};Trusted_Connection=True")/*.LogTo(Console.WriteLine)*/;}publicrecordTestRow(intId,int?Col1){publicstaticImmutableArray<TestRow>CreateTestRows()=>[new(2,null)];}
Activity
Fix negated nullable comparison in a CASE condition (dotnet#39084)