我正在使用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]
答案 0 :(得分:0)
无法说明错误是什么,但第二次显示正确的值。实体框架生成的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