Socardo
Socardo

Reputation: 520

DbSpatialServices.Default.Within method for DbGeography

In EF 6 I can use DbSpatialServices.Default.Within but with DbGeometry types only as parameters, how can I use the Within function with DbGeography type?

SQL Server 2012 has a new spatial function STWithin which accepts DbGeography and DbGeometry.

Namespace: System.Data.Entity.Spatial Assembly: EntityFramework (in EntityFramework.dll)

Upvotes: 0

Views: 488

Answers (1)

Jon Bellamy
Jon Bellamy

Reputation: 3373

There's no direct route, but there are simple yet clever workarounds.

Use the following:

DbGeography geog = blah;
<your stored data>.Where(x => x.<Your Geography Column>.Intersects(geog) && x.<Your Geography Column>.Difference(geog).IsEmpty == true)

The call to Intersects ensures use of any valid SpatialIndex whilst the final Difference clause removes any objects that are not totally within the given DbGeography instance.

Of course, if you're just looking for Points within a Polygon, it would be wise to exclude the Difference clause for performance.

Upvotes: 1

Related Questions