简体   繁体   English

LINQ to Entity不支持DbGeography条件

[英]LINQ to Entity does not support DbGeography conditions

I'm using .NET 4.5 and EF 6.0 (also tried with 6.1.3). 我正在使用.NET 4.5和EF 6.0(也尝试使用6.1.3)。 I have Location geography column in an Entities table ( System.Data.Entity.Spatial.DbGeography ). 我在实体表( System.Data.Entity.Spatial.DbGeography )中有Location geography列。

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

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

In LINQ I'm trying to select all entities which are inside a specified area. 在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();

And this query returns me an error: 这个查询给我一个错误:

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.

In case this is true: 如果是这样的话:

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

How did this work in examples over the web? 这是如何在网上的例子中工作的?

UPD. UPD。 The same problem using Intersects() 使用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();

You're likely to get the same, if not better performance using STIntersects() or STWithin() - or their EF equivalent; 使用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();

Use 'Intersects' if you want all Locations which are wholly OR partially in the region. 如果您想要所有完全或部分位于该区域的位置,请使用“相交”。 Use 'Within' if you only want those wholly within the region. 如果您只想要那些完全在该地区内的人,请使用'内'。

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

In my case my Linq query would not allow me to use: 在我的情况下,我的Linq查询不允许我使用:

x.Intersects(region) == true

I had to change this to 我不得不改变它

x.Intersects(region).IsTrue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM