加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:55:19 所属栏目:百科 来源:网络整理
导读:我正在使用.NET 4.5和EF 6.0(也尝试使用6.1.3). 我在实体表(System.Data.Entity.Spatial.DbGeography)中有Location geography列. using System.Data.Spatial; //also tried Entity onepublic class Entity{ public DbGeography Location {get;set;}} 在LINQ
我正在使用.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

这是如何在网上的例子中工作的?

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读