DbGeography 相交

DbGeography Intersects

我正在使用 DbGeographyEntity Framework 6 使用此模型:

public class County
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Code { get; set; }

    public DbGeography Area { get; set; }

}

然后我尝试执行 Intersects 方法,如下所示:

public County GetCurrentCounty(double latitude, double longitude)
{  
    var point = DbGeography.PointFromText(
        "POINT("
        + longitude.ToString(CultureInfo.InvariantCulture) + " "
        + latitude.ToString(CultureInfo.InvariantCulture) + ")",
        4326);

    var area = db.Counties.FirstOrDefault(x =>
        point.Intersects(x.Area));


    var area1 = db.Counties.FirstOrDefault(x =>
        x.Area.Intersects(point));
}

但是,对于这两种方法,创建的查询看起来都是这样的。我可以做些什么来不 select 整个 table 而是在数据库中执行查询吗?

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Code] AS [Code], 
    [Extent1].[Area] AS [Area]
    FROM [Election].[County] AS [Extent1]

不能说是什么错误,但第二次显示了正确的值。 T-SQL 由 Entity Framework:

生成
SELECT TOP (1) 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[Code] AS [Code], 
[Extent1].[Area] AS [Area]
FROM [Election].[County] AS [Extent1]
WHERE ([Extent1].[Area].STIntersects(@p__linq__0)) = 1


-- p__linq__0: 'POINT (10.0000000 32.0000000)' (Type = Object)

可以这样手动测试:

declare @p__linq__0 varchar(max)
set @p__linq__0 = 'POINT (10.0000000 32.0000000)' 

SELECT TOP (1) 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Code] AS [Code], 
    [Extent1].[Area] AS [Area]
    FROM [Election].[County] AS [Extent1]
    WHERE ([Extent1].[Area].STIntersects(@p__linq__0)) = 1

可在此处找到更多信息:

https://docs.microsoft.com/en-us/sql/t-sql/spatial-geometry/stintersects-geometry-data-type