The System.Data.Spatial.DbGeography.Distance method in C# returns the distance between two DbGeography objects in meters by default. However, you can specify a different distance unit by using the DbGeography.Distance overload that takes a System.Data.Spatial.DistanceUnit parameter.
Here's an example:
using System.Data.Entity.Spatial; DbGeography location1 = DbGeography.FromText("POINT(-122.3283 47.6097)"); DbGeography location2 = DbGeography.FromText("POINT(-74.0059 40.7128)"); double distanceInMeters = location1.Distance(location2).Value; // default unit is meters double distanceInKilometers = location1.Distance(location2, DistanceUnit.Kilometer).Value; double distanceInMiles = location1.Distance(location2, DistanceUnit.Mile).Value; In this example, we define two DbGeography objects location1 and location2 representing two points on the globe. We then calculate the distance between these two points using the DbGeography.Distance method and store the result in distanceInMeters, which is in meters by default.
We can also specify a different distance unit by using the DbGeography.Distance overload that takes a DistanceUnit parameter. In this example, we calculate the distance in kilometers and miles by passing DistanceUnit.Kilometer and DistanceUnit.Mile as the second parameter, respectively.
Note that the DbGeography.Distance method can only be used with DbGeography objects, which are provided by the System.Data.Entity.Spatial namespace. If you are not using Entity Framework or do not have access to System.Data.Entity.Spatial, you can use a third-party library like GeoCoordinate.NET to perform distance calculations.
"DbGeography.Distance units in SQL Server"
// Code Implementation: // The result is in meters by default in SQL Server var distanceInMeters = pointA.Distance(pointB);
"DbGeography.Distance units conversion"
// Code Implementation: // Convert distance from meters to kilometers var distanceInKilometers = pointA.Distance(pointB) / 1000.0;
"DbGeography.Distance units in Entity Framework"
// Code Implementation: // Distance is returned in meters by default in Entity Framework var distanceInMeters = context.Entities.Select(e => e.Location.Distance(targetLocation));
"DbGeography.Distance units in LINQ to Entities"
// Code Implementation: // LINQ to Entities returns distance in meters by default var distances = from entity in context.Entities select entity.Location.Distance(targetLocation);
r-caret addressbook ups ng-bootstrap return-value onclick swagger-ui ssrs-2008-r2 maatwebsite-excel robotframework