LINQ to Entities - Rounding to the nearest dollar, nearest half-dollar, or not at all?

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.

Up vote 2 down vote favorite share g+ share fb share tw.

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 => 133.15 ID 2: 133.15 => 133.158 public IQueryable 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... c# linq entity-framework linq-to-entities link|improve this question edited 3.159 at 17:54Aducci4,8741414 asked 3.159 at 16:47Greg Snider134.

The way you're looking to have them rounded here. But yes - Math.Round() doesn't do this out of the box for you. – 48klocs Feb 16 at 16:56 I don't believe you can create extension methods within a LINQ query.

The CustomPrice and Retail properties are decimals. Yeah, I wish .Round() would have some more options for me. – Greg Snider Feb 16 at 17:17 You can do whatever you want inside of a Linq query; if you're talking Linq to Entities/Linq to SQL then yes, you're quite a bit more restricted in what you can do inside of the query.

If you reify that anonymous type into a defined type, you could fluently perform the rounding after the data's been retrieved. – 48klocs Feb 16 at 17:27 "If you reify that anonymous type into a defined type, you could fluently perform the rounding after the data's been retrieved" - How would I go about doing this? I can create a defined type for it.

How would I perform the rounding after the data has been received? I would like to have this contained within this one function because a lot of different areas and websites will be using it. – Greg Snider Feb 16 at 17:48.

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 Price + 1 - cents)) select new { FinalPrice = roundingId == 0? Ps. Price : (roundingId == 1?

UpToDollar : upToHalfDollar) }.

Perfect! Thank you very much for this. – Greg Snider Feb 16 at 18:10 Nice, that's an elegant solution.

– Grant H. Feb 16 at 18:20.

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; } }.

Unfortunately you cannot call a method from within LINQ. I tried your example and received: LINQ to Entities does not recognize the method 'System. Decimal GetPrice(System.

Decimal, Int32)' method, and this method cannot be translated into a store expression. – Greg Snider Feb 16 at 17:15 1 Ah, you CAN call a method from within LINQ, but not LINQ to Entities, or LINQ to SQL, etc. – Grant H. Feb 16 at 17:17 I will update my question to add that information.

– Greg Snider Feb 16 at 17:18 In that case, I'd recommend you just perform the operation after the query has returned. I don't see another option without simply nesting the inline bools as you have done above. – Grant H.

Feb 16 at 17:19 Nesting inline bools would work for me (I don't mind if it is a little messy), but combining that with Rounding to Half-dollar is giving me much trouble... – Greg Snider Feb 16 at 17:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send