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

Posted on 2008-11-03
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
    LVL 7

    Accepted Solution

    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

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now