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

Discrepancy between database and C# around null coalescing #12673

Closed
roji opened this issue Jul 14, 2018 · 2 comments
Closed

Discrepancy between database and C# around null coalescing #12673

roji opened this issue Jul 14, 2018 · 2 comments

Comments

@roji
Copy link
Member

roji commented Jul 14, 2018

npgsql/efcore.pg#515 detailed a PostgreSQL FTS related issue around null coalescing which led me to think about the problem in general.

The following code demonstrates a discrepancy between how C# and SQL handle nulls when concatenating strings:

var blogs = new[] { new Blog { Name = null } };

ctx.Blogs.AddRange(blogs);
ctx.SaveChanges();

var dbCount = ctx.Blogs.Count(b => b.Name + "wat" == "wat");
var memoryCount = blogs.Count(b => b.Name + "wat" == "wat");

Console.WriteLine($"DB Count: {dbCount}, memory count: {memoryCount}");

While C#'s concatenation operator automatically coalesces nulls to empty strings, SQL doesn't (on PostgreSQL and SQLite, I'm pretty sure this is standard behavior). This forces users to systematically use the C# null coalescing operator ??.

I'm not sure if there are many other examples of C# operators which implicitly coalesce in this way, but I wonder if this is something that EF Core should fix, with the general goal of making server-side execution as close as possible client-side execution.

Note that fixing this would in principle be a breaking change, as current code is written with the expectation that coalescing doesn't occur.

@divega
Copy link
Contributor

divega commented Jul 17, 2018

EF Triage: We will look into adding coalesce operator for nullable strings in concatenation.

@smitpatel
Copy link
Contributor

Duplicate of #3836

@smitpatel smitpatel marked this as a duplicate of #3836 Mar 16, 2020
@smitpatel smitpatel removed this from the Backlog milestone Mar 16, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
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