-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Given the following model
public class Foo
{
public int Id { get; set; }
public string Address { get; set; }
public string Postcode { get; set; }
public bool Deleted { get; set; }
public bool NeedsUpdating { get; set; }
}Address and postcode are both optional/nullable.
When writing a query to pull all Foos where NeedsUpdating is true and the address or postcode are not null, changing == false to ! results in a different query.
I am unsure if this is a bug, a language quirk or something I am missing.
Queries:
var foo = await _db.Foos
.Where(c => c.Deleted == false)
.Where(c => c.NeedsUpdating)
.Where(x => string.IsNullOrEmpty(x.Postcode) == false || string.IsNullOrEmpty(x.Address) == false)
.AnyAsync();results in the following SQL query:
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Foos] AS [c]
WHERE (([c].[Deleted] = CAST(0 AS bit)) AND ([c].[NeedsUpdating] = CAST(1 AS bit))) AND ((([c].[Postcode] IS NULL OR (([c].[Postcode] = N'') AND [c].[Postcode] IS NOT NULL)) AND (CAST(0 AS bit) = CAST(1 AS bit))) OR (([c].[Address] IS NULL OR (([c].[Address] = N'') AND [c].[Address] IS NOT NULL)) AND (CAST(0 AS bit) = CAST(1 AS bit))))) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
ENDthis does not yield the correct SQL query.
However, when changing == false to !, it works:
var foo = await _db.Foos
.Where(c => c.Deleted == false)
.Where(c => c.NeedsUpdating)
.Where(x => !string.IsNullOrEmpty(x.Postcode) || !string.IsNullOrEmpty(x.Address))
.AnyAsync();results in the following SQL query:
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Foos] AS [c]
WHERE (([c].[Deleted] = CAST(0 AS bit)) AND ([c].[NeedsUpdating] = CAST(1 AS bit))) AND (([c].[Postcode] IS NOT NULL AND (([c].[Postcode] <> N'') OR [c].[Postcode] IS NULL)) OR ([c].[Address] IS NOT NULL AND (([c].[Address] <> N'') OR [c].[Address] IS NULL)))) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
ENDFurther technical details
EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: ASP.NET Core 3.0 (sdk 3.0.100)
Operating system: Windows 10 Pro 1903
IDE: Visual Studio 2019 16.3
Reactions are currently unavailable