有没有办法在没有Linq to Entities的情况下使用SqlSpatialFunctions?

Jer*_*rry 5 c# linq sql-server linq-to-entities spatial

System.Data.Entity.SqlServer.SqlSpatialFunctions类定义:

包含将Linq中的SqlServer方法公开给实体的函数存根.

我知道Linq to Entities是专门提到的,但我想知道是否可以在我的后端代码中复制某些功能.

我有一个帐户模型,延迟加载多边形集合.每个Polygon都包含一个名为Location的DbGeography类型属性.

我试图让所有多边形都具有某些特定点(它还具有延迟加载属性Address,它具有名为Location的DbGeography类型属性).

我可以这样做:

var matchedPolygons =
    account.Polygons.Where(
       x =>
          point.Address.Location.Intersects(x.Polygon.Location)).ToList();
Run Code Online (Sandbox Code Playgroud)

哪个工作正常.

为了尝试提高性能,我认为稍微减少Polygon数据是个好主意.

var matchedPolygons =
    account.Polygons.Where(
       x =>
          point.Address.Location.Intersects(SqlSpatialFunctions.Reduce(x.Polygon.Location, 100))).ToList();
Run Code Online (Sandbox Code Playgroud)

但是,这会引发以下System.NotSupportedException异常:

只能从LINQ到实体调用此函数.

我知道我可以使用上面的Reduce方法直接从我的存储库层检索多边形,但由于我正在使用延迟加载并且已经可以使用多边形集合,我想在这个阶段可能有一种使用SqlSpatialFunctions的方法.

Eri*_*ric 0

我唯一能想到的就是一个粗俗的黑客行为。它确实有效,但我并不声称它优雅。

加入这些内容(同时提供参考文献):

using System.Data.Entity.Spatial;
using System.Data.SqlTypes;
Run Code Online (Sandbox Code Playgroud)

将 DbGeography 转换为 SqlGeography:

    Microsoft.SqlServer.Types.SqlGeography sqlGeography = Microsoft.SqlServer.Types.SqlGeography.STGeomFromWKB(new SqlBytes(geog.AsBinary()), mydbgeog.CoordinateSystemId);
Run Code Online (Sandbox Code Playgroud)

如果将其保留为 SqlGeography 也许没问题,但如果不将其转换回来。

sqlGeography = sqlGeography.Reduce(50);

mydbgeog = DbGeography.FromBinary(sqlGeography.STAsBinary().Value);
Run Code Online (Sandbox Code Playgroud)

这是我所知道的在 DbGeography 和 SqlGeography 之间来回切换的最快方法。再说一次,它很恶心,需要一个额外的库,但老实说,SqlGeography 中有很多东西对于大型 GIS 应用程序来说可能需要。