How to use DbGeography.Filter in Linq with Entity Framework 5?

徘徊边缘 提交于 2019-12-06 08:16:53

问题


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:


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.



来源:https://stackoverflow.com/questions/21243403/how-to-use-dbgeography-filter-in-linq-with-entity-framework-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!