Reputation: 83
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.Nullable
1[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?
Upvotes: 5
Views: 2310
Reputation: 5632
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);
Upvotes: 9
Reputation: 338
Here you are trying to perform math.sqrt against the sql database which is not supported.
You would need to either use SQLFunctions or perform a ToList() after dbContext.Places. However, depending on how many places you bring back this would be inefficient.
var place= dbContext.Places.ToList().FirstOrDefault(x => Math.Sqrt(x.Lat) > 0);
Upvotes: 0