dotnet / dotnet/SqlClient

SqlClient truncates double values used in parameters

Open
#2,051 5 comments 0 reactions 1 assignee Claimed by @paulmedynski View on GitHub
Repro Available :heavy_check_mark:
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 18h
Merged PRs (30d)
69

Description

## Double.Epsilon value gets saved and retrieved from the sql server database, but using it in the where clause does not return the data.

### sample code

```C#
public class Blog
{
public int Id { get; set; }
public double DoubleProp { get; set; }

public float FloatProp { get; set; }
}
public static void Main()
{
using (var db = new BloggingContext())
{

db.Database.EnsureDeleted();
db.Database.EnsureCreated();

db.Blogs.Add(new Blog
{
FloatProp = float.Epsilon, // const value: 1.401298E-45
DoubleProp = double.Epsilon // const value: 4.94065645841247E-324
});

db.SaveChanges();

var fEpsilon = float.Epsilon;
var dEpsilon = double.Epsilon;

var blog = db.Blogs.First();

var bd = blog.DoubleProp == dEpsilon;// true
var bf = blog.FloatProp == fEpsilon; // true

// no data
var newBlog = db.Blogs.Where(x => x.DoubleProp == double.Epsilon).ToList();

// returns 1 row when parameterized.
//var newBlog = db.Blogs.Where(x => x.DoubleProp == dEpsilon).ToList();

// returns 1 row.
//var newBlog = db.Blogs.Where(x => x.DoubleProp == float.Epsilon).ToList();

// returns 1 row.
//var newBlog = db.Blogs.Where(x => x.DoubleProp == fEpsilon).ToList();

Console.WriteLine("======>" + newBlog.Count);
}
```

### Ef core generates below Sql query

```SQL
SELECT [b].[Id], [b].[DoubleProp], [b].[FloatProp]
FROM [Blogs] AS [b]
WHERE [b].[DoubleProp] = 4.9406564584124654E-324
```

- where clause with parameterized query returns the result.
- where clause with constant value of float.Epsilon also returns the result.

why double.Epsilon case is not handled?

Is there a miss in type casting the double literals while query translation?

### Include provider and version information

EF Core version: 7.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: win 11
IDE: Visual Studio 2022 17.6.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.