Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 576
Bas de Raad Avatar asked Nov 15 '25 20:11

Bas de Raad


1 Answers

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.

like image 160
Bas de Raad Avatar answered Nov 17 '25 11:11

Bas de Raad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!