【发布时间】:2016-05-11 12:52:26
【问题描述】:
我有一些记录,我正在尝试使用 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();
}
错误堆栈
在 KittyHawk.Domain.Services.LocationService.c__DisplayClass3_0.b__0(LocationEntity l) 在 D:\Development\manteo\KittyHawk.Domain\Services\LocationService.cs:line 40 在 System.Linq.Enumerable.WhereSelectEnumerableIterator
2.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 源) 在 D:\Development\manteo\KittyHawk.Domain\Services\LocationService.cs:line 39 中的 KittyHawk.Domain.Services.LocationService.GetAllByCoordinates(双经度、双纬度、双倍距离) 在 D:\Development\manteo\KittyHawk\Controllers\LocationController.cs:line 39 中的 KittyHawk.Controllers.LocationController.GetNearbyLocations(双经度、双纬度、双倍距离) 在 lambda_method(闭包,对象,对象 []) 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c__DisplayClass10.b__9(对象实例,对象 [] 方法参数) 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象实例,对象 [] 参数) 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext 控制器上下文,IDictionary`2 参数,CancellationToken 取消令牌)
【问题讨论】:
标签: c# sql-server entity-framework linq spatial