WAQS: Math.Round

7 reasons to use WAQS

WAQS documentation

 

I recently had a surprise with .NET:

Math.Round(3.5) = 4 (expected) but Math.Round(2.5) = 2.

 

Indeed, by default, Math.Round uses MidpointRounding.ToEven option.

 

However, SQL Round uses the AwayFromZero mode.

So WAQS only uses it with MidpointRounding.AwayFromZero option and uses arithmetic calculation for MidpointRounding.ToEven.

Like this, we have the same behavior when you get calculated properties in memory or in DB.

 

Now the question is: how to generate WAQS code in PCL client when you use MidpointRounding option in specifications?

Indeed, MidpointRounding does not exist in PCL (with .NET 4.5, SL5, W8, WP8).

So WAQS adds it and adds associated Round methods in a MathExtensions class.

Then, when WAQS duplicates the code on the client, it replaces Math.Round calls with MidpointRounding option to calls to MathExtension methods.

 

For querying, MathExtension class has an attribute of type EquivalentTypeAttribute with typeof(Math) as constructor parameter.

With it, when you use it in LINQ To WAQS queries, the query serialization sent to the server uses Math.Round method and so it works fine.

This entry was posted in 16868, 18204, 7671, 7672. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>