来自 System.Data.Entity.SqlServer.SqlSpatialFunctions
类定义:
包含将 Linq 中的 SqlServer 方法公开给实体的函数 stub 。
我知道专门提到了 Linq to Entities,但我想知道是否可以在我的后端代码中复制某些功能。
我有一个帐户模型,其中包含延迟加载的多边形集合。每个 Polygon 都包含一个名为 Location 的 DbGeography 类型属性。
我正在尝试获取所有与特定点相关的多边形(它也具有延迟加载的属性地址,它具有称为 Location 的 DbGeography 类型属性)。
我可以这样做:
var matchedPolygons =
account.Polygons.Where(
x =>
point.Address.Location.Intersects(x.Polygon.Location)).ToList();
效果很好。
为了尝试提高性能,我认为稍微减少多边形数据是个好主意。
var matchedPolygons =
account.Polygons.Where(
x =>
point.Address.Location.Intersects(SqlSpatialFunctions.Reduce(x.Polygon.Location, 100))).ToList();
然而,这会引发以下 System.NotSupportedException 异常:
此函数只能从 LINQ to Entities 调用。
我知道我可以使用上面的 Reduce 方法直接从我的存储库层检索多边形,但是由于我使用的是延迟加载并且我已经可以使用多边形集合,所以我认为可能有一种方法可以使用 SqlSpatialFunctions 在此阶段。
最佳答案
这是我唯一能想出的粗暴技巧。它有效,但我不认为它优雅。
加入这些包括(也做引用):
using System.Data.Entity.Spatial;
using System.Data.SqlTypes;
将 DbGeography 转换为 SqlGeography:
Microsoft.SqlServer.Types.SqlGeography sqlGeography = Microsoft.SqlServer.Types.SqlGeography.STGeomFromWKB(new SqlBytes(geog.AsBinary()), mydbgeog.CoordinateSystemId);
如果将其保留为 SqlGeography 可能没问题,但如果不将其转换回来。
sqlGeography = sqlGeography.Reduce(50);
mydbgeog = DbGeography.FromBinary(sqlGeography.STAsBinary().Value);
It's the fastest way I know of to toss back and forth between DbGeography and SqlGeography .同样,它很粗糙,需要一个额外的库才能到位,但老实说,SqlGeography 中有很多大型 GIS 应用程序可能需要的东西。
关于c# - 有没有办法在没有 Linq to Entities 的情况下使用 SqlSpatialFunctions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28192176/