0
votes

I've got some records that I'm trying to query with EF 6 based on distance. I've got a geography column and have used the DbGeography classes in .NET. I'm getting a Null Reference Exception when I execute the query. Ideas?

Method

public IList<Location> GetAllByCoordinates(double longitude, double latitude, double distance)
    {
        var geoPoint = DbGeography.PointFromText($"POINT({longitude} {latitude})", 4326);

        return _repo.GetAll()
            .Select(l => new Location
            {
                Id = l.Id,
                Longitude = l.Longitude,
                Latitude = l.Latitude,
                Elevation = l.Elevation,
                Distance = l.Geography.Distance(geoPoint).Value
            }).ToList();
    }

Error Stack

at KittyHawk.Domain.Services.LocationService.<>c__DisplayClass3_0.b__0(LocationEntity l) in D:\Development\manteo\KittyHawk.Domain\Services\LocationService.cs:line 40 at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at KittyHawk.Domain.Services.LocationService.GetAllByCoordinates(Double longitude, Double latitude, Double distance) in D:\Development\manteo\KittyHawk.Domain\Services\LocationService.cs:line 39 at KittyHawk.Controllers.LocationController.GetNearbyLocations(Double longitude, Double latitude, Double distance) in D:\Development\manteo\KittyHawk\Controllers\LocationController.cs:line 39 at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

1

1 Answers

0
votes

check to make sure geoPoint isn't a null. then, make sure that all of your location points have a long/lat. I have a feeling that you have some locations in your database that doing have them?