The behavior of the following are different and it's hard to manage, depending on if the query was executed or not:
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
}
Of course my actual code perform an operation on x. this is for simplicity.
I know there is Math.Round with MidpointRounding but it's not supported by Linq to Entities:
LINQ to Entities does not recognize the method 'Double Round(Double, System.MidpointRounding)' method, and this method cannot be translated into a store expression.
My question is, other than executing the query and rounding in memory, is there a way to have the same behavior in c# and Linq to Entities?
Is there a way to set the default behavior of Math.Round to always use MidpointRounding.AwayFromZero?