【问题标题】:Entity Framework Spatial Query Throws Null Reference Exception实体框架空间查询引发空引用异常
【发布时间】: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.WhereSelectEnumerableIterator2.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


    【解决方案1】:

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

    【讨论】:

    • geoPoint 不为空,一旦创建它就有一个值。此外,数据库中的每个位置都有一个地理字段。
    • 实际上经过进一步检查,我们以某种方式在表中获得了一条空记录,这导致查询失败。去图吧。
    猜你喜欢
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    相关资源
    最近更新 更多