I am currently working on an application that will retrieve other users’ locations based on distance.
I have a database that store all the user location information in latitude and longitude.
Since the calculation of distance between these two pairs of latitude and longitude is quite complicated, I need a function to handle it.
from a in db.Location.Where(a => (calDistance(lat, longi, Double.Parse(a.latitude), Double.Parse(a.longitude)))<Math.Abs(distance) )) {...}
However, I got the following error: LINQ to Entities does not recognize the method and this method cannot be translated into a store expression.
I don’t know how to translated it into a store expression and also, the calculation also need the math library.
Is there any method that i can do to let the LINQ expression call my own function?
Maybe there are other ways to achieve my goal, can anyone help?
I don’t really know LINQ, but assuming that you can only send simple constraints in the query, I would construct a method that basically does the inverse of calDistance – take a coordinate and a distance and convert it into a bounding box with a minimum longitude, maximum longitude, minimum latitude, and maximum latitude.
You should be able to construct a simple query that will serve your purposes with those constraints.
something like (using Java here):
Then you could construct a query.
This would give you a box of points rather than a radius of points, but it would be few enough points that you could then process them and throw out the ones outside your radius. Or you might decide that a box is good enough for your purposes.