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 Query

More Query from same tag