score:2

Accepted answer

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

var prices = from ps in ObjectContext.Products
             let cents = ps.Price % 1
             let upToDollar = cents > 0 ? (ps.Price + 1 - cents) : ps.Price
             let upToHalfDollar = cents == 0 ? ps.Price : (cents < 0.5 ? (ps.Price + 0.5 - cents) : (ps.Price + 1 - cents))
             select new
             {
               FinalPrice = roundingId == 0 ? ps.Price : (roundingId == 1 ? upToDollar : upToHalfDollar)
             };

score:1

You can do it all inline if you want, but honestly that seems like it'd be hard to read. Try this:

int roundingId = 0; //0 = no rounding. 1 = dollar rounding. 2 = half-dollar rounding.

    var prices = from ps in ObjectContext.Products
    select new
    {
         FinalPrice = GetPrice((ps.IsCustomPrice ? ps.CustomPrice : ps.Retail), roundingId),
    }

private double GetPrice(double price, int roundingOption)
{
    switch (roundingOption)
    {
        case 0:
        //Do stuff
        break;
        case 1:
        //Do stuff
        break;
        case 2:
        //Do stuff
        break;
    }
}

Related Articles