Math.Round in linq to entities vs linq to objects

c# entity-framework entity-framework-6

Question

Depending on whether the query was run or not, the behavior of the following changes and is difficult to manage:

using (var db = new DbContext()) 
{
    db.Entities.Select(x => Math.Round(0.5)).First(); // return 1
    db.Entities.ToList().Select(x => Math.Round(0.5)).First(); // returns 0
    db.Entities.AsEnumerable().Select(x => Math.Round(0.5)).First(); // returns 0
} 

Obviously, the actual code I use operates onx . this is for ease of use.

I am aware of Rounding using the midpoint in math, but Linq to Entities does not support it.

LINQ to Entities does not recognize the method 'Double Round(Double, System.MidpointRounding)' method, and this method cannot be translated into a store expression.

I want to know if there is a method to have the same behavior in c# and Linq to Entities other than running the query and rounding in memory.

Is it possible to modify Math's default settings? Round to utilize alwaysMidpointRounding.AwayFromZero ?

1
3
10/31/2018 7:42:52 PM

Accepted Answer

ZZZ_tmp
0
10/31/2018 1:13:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow