why not switch to 100% SQL, as this is the best way to do the calculations and just get a table filled with distances?
from existing answer
CREATE FUNCTION dbo.udf_Haversine(@lat1 float, @long1 float, @lat2 float, @long2 float) RETURNS float BEGIN DECLARE @dlon float, @dlat float, @rlat1 float, @rlat2 float, @rlong1 float, @rlong2 float, @a float, @c float, @R float, @d float, @DtoR float SELECT @DtoR = 0.017453293 SELECT @R = 3937
and is used as:
var table = from r in db.VenuePostCodes select new { lat = r.Latitude, lng = r.Longitude, name = r.Name, distance = db.udf_Haversine( r.Latitude,r.Longitude, r.Latitude,r.Longitude2) };
but itโs best to always have everything in SQL, so your hosting server is smaller, just add VIEW to your SQL and call this view, imagine:
SELECT latitude, longitude, name, latitude1, longitude2, postcode, udf_Haversine(latitude, longitude, latitude2, longitude2) AS distance FROM venuepostcodes ORDER BY distance
and use LINQ to directly call this view.
balexandre
source share