c# - 使用 distance() 方法进行空间 Linq 查询

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

我正在尝试从 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
         };

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

最佳答案

看来您已在 WKT 中针对您的点切换了经度和纬度。这是一个演示的简单示例:

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

关于c# - 使用 distance() 方法进行空间 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032744/

相关文章:

c# - 在静态类中有状态是不好的做法吗?

c# - Entity Framework 搜索多个词

c# - 在 WPF 中对分类数据进行分组

c# - .NET 有 'Mandatory Parameter' 这样的东西吗?

sql - 为什么 SQL 评估的 WHERE 子句为 False?

java - 如何在网络计算机上连接SQL Server 2017(即监听本地端口)

.net - Entity Framework 和分离对象

c# - 使用 WCF 数据服务将其发送到客户端后,部分类中的自定义属性不存在

c# - OnMouseEnter 旋转变换

sql-server - SQL Server 复制触发器