How to use DbGeography.Filter in Linq with Entity

2019-04-12 02:23发布

With Entity Framework 5 it is possible to use the SQL Server Spatial procedures in Linq queries.

For example, using a DbGeography object, you can use the "Buffer()" method which will translate to STBuffer in SQL Server. Same way, Intersects() will translate to STIntersects.

This is an example query that works:

  var point = DbGeography.FromText(string.Format("POINT({1} {0})", latitude, longitude), 4326);
  var query = from person in persons
              let region = point.Buffer(radius)
              where person.Location.Intersects(region)
              select person;

I would like to use the Filter possibility (since this can speed up your queries if accuracy is not your main concern as pointed out here: http://www.pauldmendoza.com/post/SQL-Server-Filter-vs-STInterects.aspx) However, I can't seem to find how to do this in EF5. Is this possible? And if yes: how?

I'm using SQL Server 2008 R2.

1条回答
Rolldiameter
2楼-- · 2019-04-12 03:21

Asked the question a bit too soon. I found this: http://msdn.microsoft.com/en-us/library/hh673622(v=vs.110).aspx

And this can be used like this:

  var point = DbGeography.FromText(string.Format("POINT({1} {0})", latitude, longitude), 4326);
  var query = from person in persons
              let region = point.Buffer(radius)
              where SqlSpatialFunctions.Filter(person.Location, region) == true
              select person; 

And this translates to the query I wanted.

查看更多
登录 后发表回答