5

I'm using .NET 4.5 and EF 6.0 (also tried with 6.1.3). I have Location geography column in an Entities table (System.Data.Entity.Spatial.DbGeography).

using System.Data.Spatial; //also tried Entity one

public class Entity
{
    public DbGeography Location {get;set;}
}

In LINQ I'm trying to select all entities which are inside a specified area.

var center = DbGeography.FromText(string.Format("POINT({0} {1})", latitude, longitude), 4326);
var region = center.Buffer(radius);
var result = db.Entities.Where(x => SqlSpatialFunctions.Filter(x.Location, region) == true).ToArray();

And this query returns me an error:

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

Additional information: The specified type member 'Location' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

In case this is true:

http://referencesource.microsoft.com/#System.Data.Entity/System/Data/Objects/SqlClient/SqlSpatialFunctions.cs

How did this work in examples over the web?

UPD. The same problem using Intersects()

var center = DbGeography.FromText(string.Format("POINT({0} {1})", latitude, longitude), 4326);
var region = center.Buffer(radius);
var result = db.Entities.Where(x => x.Location.Intersects(region) == true).ToArray();
dr11
  • 5,166
  • 11
  • 35
  • 77

3 Answers3

0

You're likely to get the same, if not better performance using STIntersects() or STWithin() - or their EF equivalent;

// SQL STIntersects() equivalent    
var result = db.Entities.Where(x => x.Intersects(region)).ToArray();

// SQL STWithin() equivalent    
var result = db.Entities.Where(x => x.Intersects(region) == true && x.Difference(region).IsEmpty == true).ToArray();

Use 'Intersects' if you want all Locations which are wholly OR partially in the region. Use 'Within' if you only want those wholly within the region.

Jon Bellamy
  • 3,333
  • 20
  • 23
  • updated post. Intersects() throws the same exception. – dr11 Sep 07 '15 at 17:40
  • @deeptowncitizen - how odd! And you're using EF6? How have you mapped the table? – Jon Bellamy Sep 07 '15 at 18:43
  • Yes, that is EF6. I had a table with Lng, Lat fields. I added Location of type Geography. And mapped this new column on Entity DbGeography. btw SQL requests work well – dr11 Sep 07 '15 at 19:17
0

In case you're using DB First approach it is necessary to update model from Model Browser, but not manually.

dr11
  • 5,166
  • 11
  • 35
  • 77
0

In my case my Linq query would not allow me to use:

x.Intersects(region) == true

I had to change this to

x.Intersects(region).IsTrue
Euan Gordon
  • 98
  • 1
  • 8