微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用.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 @R_691_4045@ion: 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.使用Intersects()的同样问题

var center = DbGeography.FromText(string.Format("POINT({0} {1})",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();

如果您想要所有完全或部分位于该区域的位置,请使用“相交”.如果您只想要那些完全在该地区内的人,请使用’内’.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐