C# 地理空间搜索-实体框架核心/PostgreSQL
我有一个存储一些事件位置的表和一个用ef core映射到该表的类:C# 地理空间搜索-实体框架核心/PostgreSQL,c#,postgresql,linq,asp.net-core,entity-framework-core,C#,Postgresql,Linq,Asp.net Core,Entity Framework Core,我有一个存储一些事件位置的表和一个用ef core映射到该表的类: public class Location { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Column(TypeName = "geography (point)")]
public class Location
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column(TypeName = "geography (point)")]
public Point Location { get; set; }
public string? Address { get; set; }
public string? City { get; set; }
public string? Region { get; set; }
public string? Country { get; set; }
public Post Post { get; set; }
public int PostId { get; set; }
}
问题Location类与Post有1-1关系。 现在,输入纬度、经度和半径xkm,我希望能够从数据库中获取该半径内的所有帖子。i、 e我要执行地理空间查询
我找到了一个解决方案,但它涉及到执行一些简单的SQL查询,我试图不惜一切代价避免这些查询。 如何使用LINQ执行此操作?解决方案来自。它增加了对地理对象的支持,允许在SQL中运行位置查询。它必须安装在DB侧。对于服务器端,必须安装以下组件 可以执行地理空间查询的相关方法称为“.IsWithinDistance”。 纬度、经度和半径必须作为点传递。将返回指定范围内的所有记录 例如:
posts = await Context.Posts
.Include(p => p.Location)
.Where(p => p.Location.Location.IsWithinDistance(location, radius * 1000)
.AsNoTracking().ToListAsync();
解决方案来自于。它增加了对地理对象的支持,允许在SQL中运行位置查询。它必须安装在DB侧。对于服务器端,必须安装以下组件
可以执行地理空间查询的相关方法称为“.IsWithinDistance”。
纬度、经度和半径必须作为点传递。将返回指定范围内的所有记录
例如:
posts = await Context.Posts
.Include(p => p.Location)
.Where(p => p.Location.Location.IsWithinDistance(location, radius * 1000)
.AsNoTracking().ToListAsync();
你在这里提到的
radius X km
是什么意思,它不是字段,你的Post
类的结构是什么?你在这里提到的radius X km
是什么意思,它不是字段,你的Post
类的结构是什么?