I need to get nearby location using (longitude, latitude )
I have a table where the locations are saved in database with longitude and latitude fields,
I want to retrieve the nearest locations, and I have Sql code and want to convert it to linq, where I'm using ASP.Net MVC5 Here is my code:
SELECT Name, ( 3959 * acos( cos( radians(24.743055) ) * cos( radians(
latitude ) ) *
cos( radians( longitude ) - radians(46.669702) ) + sin( radians(24.743055)
)*
sin( radians( latitude ) ) ) ) AS distance
FROM Event
ORDER BY distance
I wrote it in linq like this:
double startlat = Convert.ToDouble(db.Users.Where(u => u.Id == 2).Select(u
=> u.latitude).Single());
double startlng = Convert.ToDouble(db.Users.Where(u => u.Id == 2).Select(u
=> u.longitude).Single());
var c = (from ev in db.Event
where (3959 * Math.Acos(Math.Cos((Math.PI * startlat / 180.0)) *
Math.Cos(Math.PI * (Convert.ToDouble(ev.latitude)) / 180.0) *
Math.Cos((Math.PI * (Convert.ToDouble(ev.longitude)) / 180.0) - (Math.PI *
(startlng) / 180.0)) + Math.Sin(Math.PI * (startlat) / 180.0)) *
Math.Sin(Math.PI * (Convert.ToDouble(ev.latitude) ) / 180.0) ) < 2500
select ev.Name).ToList();
but I get this error:
LINQ to Entities does not recognize the method 'Double Acos(Double)'
method, and this method cannot be translated into a store expression.
I tried to use "public static double ToRadians" and pass the value, but it didn't work because it's a static
Any Ideas ?