Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Equivalent of SQL Server STDistance

I can use the following SQL to calculate the distance between a fixed location and the location against the venues in the database.

SELECT Location.STDistance(geography::Point(51, -2, 4326)) * 0.00062137119 FROM Venues

Please note the distance returned is in miles and the Location field is a geography type.

I was wondering what is the equivalent of this in .NET which would return the same values. This method would have the following signature:

public static double Distance(location1Latitude, location1Longitude, location2Latitude, location2Longitude) {
    return ...;
}

I know I could call the database method in .NET but I don't wish to do this. I'm hoping there is a formula to calculate the distance. Thanks

like image 430
nfplee Avatar asked Apr 09 '26 01:04

nfplee


1 Answers

I believe you can simply add Microsoft.SqlServer.Types.dll as a reference and then use the SqlGeometry type like any other .NET type, including calling the STDistance method.

like image 128
Allon Guralnek Avatar answered Apr 11 '26 14:04

Allon Guralnek