2015-09-06 91 views
4

我正在使用.NET 4.5和EF 6.0(也试过6.1.3)。 我有位置地理列在实体表(System.Data.Entity.Spatial.DbGeography)。LINQ to Entity不支持DbGeography条件

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 

如何做这项工作在网上的例子吗?

UPD。同样使用的问题相交()

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(); 

回答

0

你可能使用,如果没有更好的性能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(); 

如果您想要完全或部分位于区域中的所有位置,请使用'相交'。如果你只想要那些完全在区域内的人,请使用'Within'。

+0

更新的帖子。 Intersects()引发相同的异常。 – deeptowncitizen

+0

@deeownowncitizen - 多奇怪!你正在使用EF6?你如何绘制表格? –

+0

是的,那就是EF6。我有一张Lng,Lat字段的桌子。我添加了地理类型的位置。并将这个新列映射到Entity DbGeography。 btw SQL请求运作良好 – deeptowncitizen

0

如果您使用DB First方法,则需要从模型浏览器更新模型,但不是手动。

0

在我的情况,我的Linq查询将不会允许我使用:

x.Intersects(region) == true 

我不得不改变这

x.Intersects(region).IsTrue