Repository navigation
Backlog
Backlog
Copy link
Description
| Environment | |
|---|---|
| PostgreSQL | 18.6 (postgres:18), session TimeZone Etc/UTC |
| .NET 10 | runtime 10.0.15, Npgsql.EntityFrameworkCore.PostgreSQL 10.0.4, EF Core 10.0.15, Npgsql 10.0.4 |
| .NET 11 | runtime 11.0.0, Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0, EF Core 11.0.0, Npgsql 11.0.0 |
| Also | macOS arm64 |
Both lines print the same output. main at 9a76cd8 has the same code.
Split from #4095 (closed) at your request. Its BigInteger part is #4143.
C# casts from decimal, double and float to an integer type truncate toward zero. PostgreSQL's casts round: half away from zero for numeric, half to even for float8/float4. SQL Server's CAST truncates, so this is specific to this provider. It also affects filters, not only projections.
| LINQ | Row | .NET | PostgreSQL | SQL |
|---|---|---|---|---|
(int)r.Decimal |
2.7m | 2 | 3 | r."Decimal"::int |
(long)r.Double |
2.7 | 2 | 3 | r."Double"::bigint |
(int)r.Single |
2.7f | 2 | 3 | r."Single"::int |
(int)r.Decimal == 2 |
2.7m | match | no match | r."Decimal"::int = 2 |
Repro
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
await using var context = new TestContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Rows.Add(new Row { Decimal = 2.7m, Double = 2.7, Single = 2.7f });
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();
// C# casts to an integer type truncate toward zero
Console.WriteLine($"(int)2.7m: .NET {(int)row.Decimal}, database {await context.Rows.Select(r => (int)r.Decimal).SingleAsync()}");
Console.WriteLine($"(long)2.7: .NET {(long)row.Double}, database {await context.Rows.Select(r => (long)r.Double).SingleAsync()}");
Console.WriteLine($"(int)2.7f: .NET {(int)row.Single}, database {await context.Rows.Select(r => (int)r.Single).SingleAsync()}");
Console.WriteLine($"(int)Decimal == 2: .NET {((int)row.Decimal == 2 ? 1 : 0)}, database {await context.Rows.CountAsync(r => (int)r.Decimal == 2)}");
public class TestContext : DbContext
{
public DbSet<Row> Rows => Set<Row>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"))
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Row
{
public int Id { get; set; }
public decimal Decimal { get; set; }
public double Double { get; set; }
public float Single { get; set; }
}Output, the same on both lines:
(int)2.7m: .NET 2, database 3
(long)2.7: .NET 2, database 3
(int)2.7f: .NET 2, database 3
(int)Decimal == 2: .NET 1, database 0
The cast is rendered as ::int in NpgsqlQuerySqlGenerator.cs#L617-L640. trunc(x)::int matches .NET. I understand this may be a deliberate trade-off (existing queries would change answers, and an expression index on x::int would no longer match), in which case a docs note would help.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Zentauer commentedon Nov 26, 2026
Hi @roji, I'd like to pick this one up, since it's close to the BigInteger division change in #4144.
My plan is to wrap the operand in trunc() when converting from decimal, double or float to an integer type, so the SQL becomes trunc(x)::int. I'd do it in the translation step, like the div() change, and add tests for both projections and filters.
Existing queries would return different results and an expression index on x::int would no longer match, so I'd target 12.0 like the other fix. Does that work for you, or would you rather keep the current behavior and document it?