LINQ to Entity Does Not Support DbGeography Conditions

I am using .NET 4.5 and EF 6.0 (also tried with 6.1.3). I have a location geography column in the 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 the objects that are inside the 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 does this work in examples over the Internet?

UPD 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();
+4
source share
3 answers

You will probably get the same, if not better, using STIntersects () or STWithin () - or their equivalent EF;

// 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();

"", , . "", , .

0

DB, , .

0

In my case, my Linq request will not let me use:

x.Intersects(region) == true

I had to change this to

x.Intersects(region).IsTrue
0
source

Source: https://habr.com/ru/post/1606177/


All Articles