dee*_*zen 5 c# geospatial spatial-query entity-framework-6
我正在使用 .NET 4.5 和 EF 6.0(也尝试过 6.1.3)。我在实体表(System.Data.Entity.Spatial.DbGeography )中有位置地理列。
using System.Data.Spatial; //also tried Entity one
public class Entity
{
public DbGeography Location {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
在 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();
Run Code Online (Sandbox Code Playgroud)
这个查询返回一个错误:
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.
Run Code Online (Sandbox Code Playgroud)
如果这是真的:
http://referencesource.microsoft.com/#System.Data.Entity/System/Data/Objects/SqlClient/SqlSpatialFunctions.cs
Run Code Online (Sandbox Code Playgroud)
这在网络示例中是如何工作的?
UPD。使用 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();
Run Code Online (Sandbox Code Playgroud)
使用 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();
Run Code Online (Sandbox Code Playgroud)
如果您想要全部或部分位于该区域的所有位置,请使用“相交”。如果您只想完全在该区域内,请使用“内部”。