I have this working code:
IEnumerable<Message> dbCities = db.Cities;
dbCities = dbCities.Where(x => GetDistanceBetweenLocations(longitude, latitude, x.Longitude, x.Latitude) <= radius);
public int GetDistanceBetweenLocations(double lon1, double lat1, double lon2, double lat2)
{
var sCoord = new GeoCoordinate(lon1, lat1);
var eCoord = new GeoCoordinate(lon2, lat2);
return Convert.ToInt32(Math.Round(sCoord.GetDistanceTo(eCoord)));
}
But it is kind of slow. I'm trying to move the logic in the DB for better performance.
I started with few things like:
dbCities = db.Cities.Where(x => new GeoCoordinate(longitude, latitude).GetDistanceTo(new GeoCoordinate(x.Longitude, x.Latitude)) <= radius);
And got: LINQ to Entities does not recognize the method 'Double GetDistanceTo(System.Device.Location.GeoCoordinate)' method, and this method cannot be translated into a store expression.
I know about the Haversine formula but could'n make it work with linq.
The project uses a SQL Server DB and code-first approach.