We have a postal code table with longitude & latitude as column, can't be changed to spatial data type due to production requirement.
Was using STDistance()
but I'm getting an error:
LINQ to Entities does not recognize the method
Therefore I have to call toList
then only use STDistance()
, which is pulling every postal code into the system & causing slow performance.
private static List<string> GetPostalCodesWithinRange( DbContext db, double latitude, double longitude)
{
var yourLocation = SqlGeography.Point(latitude, longitude, 4326);
var query = from postal in db.POSTALS
.Where(x => x.LATITUDE != null ||
x.LONGITUDE != null)
.ToList()
let distance = SqlGeography.Point((double)postal.LATITUDE.Value, (double)postal.LONGITUDE.Value, 4326).STDistance(yourLocation)
.Value
where postal.LATITUDE != null && postal.LONGITUDE != null && distance < 3000
orderby distance
select postal.POSTAL_CODE;
return query.Distinct().ToList();
}
Currently the Postals
toList()
method only filters out the null Latitude & Longitude
db.POSTALS.Where(x => x.LATITUDE != null || x.LONGITUDE != null).ToList()
Since we are only checking the longlat within 3km, now we would like to optimize the query by pulling only the longitude & latitude that reside within 3km square border, then only filter by radius using the method above.
May I ask what's way to get the corner longlat points that's 1.5km away from the center?