簡體   English   中英

LINQ to Entity不支持DbGeography條件

[英]LINQ to Entity does not support DbGeography conditions

我正在使用.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})", latitude, longitude), 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();

如果您想要所有完全或部分位於該區域的位置,請使用“相交”。 如果您只想要那些完全在該地區內的人,請使用'內'。

如果您使用的是DB First方法,則需要從模型瀏覽器更新模型,但不能手動更新。

在我的情況下,我的Linq查詢不允許我使用:

x.Intersects(region) == true

我不得不改變它

x.Intersects(region).IsTrue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM