【问题标题】:Spatial Linq query with distance() method使用 distance() 方法进行空间 Linq 查询
【发布时间】:2014-04-12 15:59:44
【问题描述】:

我正在尝试从 SQL Server 数据库中获取特定半径内的位置。当我使用数据库中的空间数据并将其传递给DBGeography.Distance() 方法时,它工作正常。但是当我尝试将坐标传递给Distance() 方法时,例如:

var sourcePoint = string.Format("POINT({0} {1})", location.lat.ToString().Replace(',','.'), location.lon.ToString().Replace(',','.'));

var origin = DbGeography.PointFromText(sourcePoint, 4326);

var result = from r in db.Locations
        where r.Coordinates.Distance(origin) <= distanceMeters
        select r;

它什么也不返回。虽然位置在定义的半径内。

这样做很好,并且能够获得半径内的位置:

var person = db.People.Single(p => p.PersonID == 2);           

var stores = from s in db.Stores
         where s.Location.Distance(person.Address) * .00062 <= 1

         select new Location
         {

         Name = s.Name,
         Latitude = s.Location.Latitude,
         Longitude = s.Location.Longitude
         };

我无法理解为什么它在第一个示例方式中不起作用。 :)

【问题讨论】:

  • 旁注:考虑使用.ToString(CultureInfo.InvariantCulture)

标签: c# sql-server entity-framework asp.net-web-api2 spatial-query


【解决方案1】:

您的观点似乎已在 WKT 中切换了经度和纬度。下面是一个简单的示例来演示:

DECLARE @g geography;
SET @g = geography::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @g.Long, @g.Lat;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多