I am trying to take the absolute value in a linq to entities query and getting an exception

I am making a call to Math.Abs() inside of my linq to entities query, e.g.
var query = from mySource
select new {absv = Math.Abs((double) mySource.myValue)}
//the query above is not necessarily compiling, I just paraphrased it, in my application I have the syntax right // but the following exception happens:
"LINQ to Entities does not recognize the method 'Double Abs(Double)' method, and this method cannot be translated into a store expression."

mySource is coming from a sql table and myValue represents a nullable column of type FLOAT

All I am trying to do is to take the absolute value of the myValue column inside of my projection, but I am getting this exception.  

Any ideas?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Refael AckermannCommented:
L2E is trying to translate the call the Math.Abs to something is can run in the DB. Unfortunatly it doesn't have a mapping so it fails.
You should be able to do a "lifted" projection after the first projection.

var q = from mySource
        select new {v = (double)mySource.myValue};
var q2 = from x in q.ToList() // ToList causes "lifting"
         select new {absv = Math.Abs(x.v)};

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
izmikeAuthor Commented:
Thanks for the comment, that does shed light on things, however this absolute value column is one of many columns that are being used to produce a report, using the "lifting" technique here seems like it will bloat the code and cause a lot of repetition.

I have opted to use a conditional here instead, I knew I could do this but preferred to actually use the ABS function if L2E supported it.  

var q = from mySource
        select new {absv = mySource.myValue <0 ? -1*mySource.myValue : mySource.myValue};

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.