Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2008-11-03
Medium Priority
Last Modified: 2013-11-11
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?
Question by:izmike

Accepted Solution

moseack earned 1500 total points
ID: 22873958
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


Author Comment

ID: 22877128
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};


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question