Entity Framework 空间查询引发空引用异常

Entity Framework Spatial Query Throws Null Reference Exception

我有一些记录,我正尝试根据距离使用 EF 6 进行查询。我有一个地理专栏,并在 .NET 中使用了 DbGeography 类。执行查询时出现空引用异常。想法?

方法

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();
    }

错误堆栈

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)

检查以确保 geoPoint 不为空。然后,确保您所有的位置点都有 long/lat。我有一种感觉,你的数据库中有一些位置确实有它们?