Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Math functions in Entity framework with mysql

I am developing ASP.NET MVC application and use Mysql with entity framework in it. But I'm having trouble with executing Linq query like this:

var place= dbContext.Places.FirstOrDefault(x => Math.Sqrt(x.Lat) > 0);

I also tried using SqlFunctions:

var place= dbContext.Places.FirstOrDefault(x => SqlFunctions.SquareRoot(x.Lat) > 0);

But either way I'm getting 'System.NotSupportedException':

Additional information: The specified method 'System.Nullable1[System.Double] SquareRoot(System.Nullable1[System.Decimal])' on the type 'System.Data.Entity.SqlServer.SqlFunctions' cannot be translated into a LINQ to Entities store expression.

Is there any way to use Square root function so that it could be translated into a LINQ to Entities expression?

like image 401
Lasha Amashukeli Avatar asked Mar 16 '23 04:03

Lasha Amashukeli


1 Answers

Entity Framework doesn't support Sqrt function:

Math Canonical Functions - list of supported functions.

So you can use Math.Pow insted:

var place= dbContext.Places.FirstOrDefault(x => Math.Pow(x.Lat,0.5) > 0);
like image 80
freshbm Avatar answered Mar 24 '23 23:03

freshbm