2

We are going from using Dapper to using Entity framework. There is this method that should return number of liters for a given Id.

Example:

|-----|--------|
| Id  | Litres |
|-----|--------|
|  1  | 2.75   |
|-----|--------|
|  1  | 2.113  |
|-----|--------|
|  2  | 1.23   |
|-----|--------|

Result should in this case be:

id: 1 SumLitres: 4.863 id: 2 SumLitres: 1.23

In the actual case there are thousands of entries and the prescision of the sum of these are different when using dapper and Linq. We can accept 3 decimal prescision, but not lower than that.

The dapper query:

const string query = @"
            SELECT Id, Sum(Litres) as Litres
            FROM Litres
            GROUP BY Id;

The linq query:

context.Litres.Select(x => new { x.Id, x.Litres})
                    .GroupBy(x => x.Id)
                    .AsNoTracking()
                    .ToList() 
                    .Select(g => new { Id = g.Key, Litres = g.Sum(s => s.Litres) })
                    .ToList();

The problem is that the results differ in second decimal place and i dont know which of these to queries gives the result with highest precision?

Anyone knows how to increase the precision or which one has the highest?

The column Litres has the datatype: real.

Moddaman
  • 2,538
  • 3
  • 23
  • 41

1 Answers1

0

It is likely that when converting from SQL Real to .Net Single there is some rounding: Sql server real datatype, what is the C# equivalent?

You could try using executing raw SQL using your entity context: https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx

Calidus
  • 1,374
  • 2
  • 14
  • 31