Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

c# - Linq to EF - Unsupported Functions

I'm making a query with Linq, backed by an Entity Framework datasource.

I'm getting the following error:

LINQ to Entities does not recognize the method 'Double Sqrt(Double)' method, and this method cannot be translated into a store expression.

Here's a simplified version of my function (my version is more complex and uses ACos, sin, cos and other C# Math class functions).

  var objects =
            from n in context.Products.Where(p => p.r == r)
            let a = Math.Sqrt((double)n.Latitude)
            where a < 5
            orderby a
            select n;

        return objects.Take(100).ToList();

I think the problem may be related to the situation that Linq to EF (and a SQL datasource) has a limited set of supported function compared to Linq to SQL. I'm relatively new to this so I'm not 100% sure.

Can anyone give me a pointer in the right direction?

Cheers,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try SquareRoot function defined in SqlFunctions

    var objects =
        from n in context.Products.Where(p => p.r == r)
        let a = SqlFunctions.SquareRoot((double)n.Latitude)
        where a < 5
        orderby a
        select n;

    return objects.Take(100).ToList();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...