c# - LINQ to Entity 不支持 DbGeography 条件

标签 c# entity-framework-6 geospatial spatial-query

我正在使用 .NET 4.5 和 EF 6.0(也尝试使用 6.1.3)。 我在实体表 (System.Data.Entity.Spatial.DbGeography) 中有 Location geography 列。

using System.Data.Spatial; //also tried Entity one

public class Entity
{
    public DbGeography Location {get;set;}
}

在 LINQ 中,我试图选择指定区域内的所有实体。

var center = DbGeography.FromText(string.Format("POINT({0} {1})", latitude, longitude), 4326);
var region = center.Buffer(radius);
var result = db.Entities.Where(x => SqlSpatialFunctions.Filter(x.Location, region) == true).ToArray();

这个查询返回一个错误:

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

Additional information: The specified type member 'Location' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

如果这是真的:

http://referencesource.microsoft.com/#System.Data.Entity/System/Data/Objects/SqlClient/SqlSpatialFunctions.cs

这在网络示例中是如何工作的?

更新。使用 Intersects() 同样的问题

var center = DbGeography.FromText(string.Format("POINT({0} {1})", latitude, longitude), 4326);
var region = center.Buffer(radius);
var result = db.Entities.Where(x => x.Location.Intersects(region) == true).ToArray();

最佳答案

使用 STIntersects() 或 STWithin() 或它们的 EF 等价物,您可能会获得相同甚至更好的性能;

// SQL STIntersects() equivalent    
var result = db.Entities.Where(x => x.Intersects(region)).ToArray();

// SQL STWithin() equivalent    
var result = db.Entities.Where(x => x.Intersects(region) == true && x.Difference(region).IsEmpty == true).ToArray();

如果您想要全部或部分位于区域内的所有位置,请使用“相交”。如果您只想要完全在区域内的那些,请使用“范围内”。

关于c# - LINQ to Entity 不支持 DbGeography 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32427924/

相关文章:

c# - 计算e数C#

当 SQL 数据库添加新行而不轮询时,C# Azure Function 触发器

mysql - 如何在MySQL中存储空间文件

mysql - 如何在 yii 模型中将 mysql spatial POINT 转换为可读格式?

c# - 属性类型更改时不更新鉴别器

solr - Solr 3.4查询GeoHash字段性能问题

c# - 输入不是有效的 Base64 字符串,因为它包含非 Base 64 字符

c# - 为什么我在这里得到 "Cannot access a closed Stream"?

c# - 什么是 modelBuilder.entity(Of x)

c# - 带有 Identity 2 和 EntityFramework 6 的 ASP.NET Core MVC(甲骨文)