I need to round FinalPrice the following ways based on an identifier:
- 0 = Not at all
- 1 = Up to the next Dollar
- 2 = Up to the next Half-dollar
Example of the expected rounding:
- ID 0: 133.15 => 133.15
- ID 1: 133.15 => 134.00
- ID 2: 133.15 => 133.50
public IQueryable<MyObj> GetPrices()
{
int roundingId = 0; //0 = no rounding. 1 = dollar rounding. 2 = half-dollar rounding.
var prices = from ps in ObjectContext.Products
select new MyObj
{
FinalPrice = (ps.IsCustomPrice ? ps.CustomPrice : ps.Retail),
}
return prices;
}
I wish I could use a custom function to do this rounding in LINQ to Entities…
This will work for you. You can clean it up for your own use, but basically you are just using the
%modulo operator to get the cents and the? :ternary operator to select the correct value