0

我正在DbGeography使用Entity 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 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Code] AS [Code], 
    [Extent1].[Area] AS [Area]
    FROM [Election].[County] AS [Extent1]
4

1 回答 1

0

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

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

于 2018-04-18T13:05:06.373 回答