【问题标题】:LINQ to Entity does not support DbGeography conditionsLINQ to Entity 不支持 DbGeography 条件
【发布时间】:2015-09-06 20:22:57
【问题描述】:

我正在使用 .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();

【问题讨论】:

    标签: c# entity-framework-6 geospatial spatial-query


    【解决方案1】:

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

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

    【讨论】:

    • 更新帖子。 Intersects() 抛出同样的异常。
    • @deeptowncitizen - 真奇怪!你使用的是EF6吗?你是如何映射表格的?
    • 是的,就是 EF6。我有一张带有 Lng、Lat 字段的表格。我添加了地理类型的位置。并将这个新列映射到 Entity DbGeography。顺便说一句,SQL 请求运行良好
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      x.Intersects(region) == true
      

      我不得不把它改成

      x.Intersects(region).IsTrue
      

      【讨论】:

        猜你喜欢
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 2012-03-03
        • 2017-06-20
        • 1970-01-01
        • 1970-01-01
        • 2011-12-07
        • 1970-01-01
        相关资源
        最近更新 更多