EF Core Spatial Data:查询并获取以米为单位的距离

Sas*_*san 6 sql-server spatial entity-framework-core asp.net-core

我可以查询并获取距客户最近的位置(有学位):

serviceQuery = serviceQuery.Where(s => s.Location.Distance(currentLocation) < <degree>)
    .OrderBy(s => s.Location.Distance(currentLocation));
Run Code Online (Sandbox Code Playgroud)

位置是NetTopologySuite.Geometries.PointC# 和 SQL Server 中的类型geography

如何在查询中提供米而不是度数,并获取米距离而不是度数?在客户端将结果度数转换为米是可以接受的。

Sas*_*san 2

我发现了问题所在。

PostgreSQL(我们现在使用的)和其他数据库可能有两种空间数据类型:几何和地理。

您可以参考这些链接以获取更多信息(链接 1链接 2)。

长话短说,如果您正在使用地球的纬度/经度并且希望计算使用米单位,则需要使用地理类型。

属性:

[Column(TypeName="geography (point)")]
public Point Location { get; set; }
Run Code Online (Sandbox Code Playgroud)

流畅的API:

builder.Entity<YourEntity>()
    .Property(e => e.Location)
    .HasColumnType("geography (point)");
Run Code Online (Sandbox Code Playgroud)

如果您不指定,则默认为用于笛卡尔坐标的几何。

另外,数据库数据的SRID必须与你查询时使用的位置相同。