I'm trying to return a list of items that are within say 50 Miles of a given location.
My table (simplified) is as follows:
- Id
- Longitude
- Latitude
- Status
- Active
I've got an initial query:
var query = db.MyTable.Where(o=> o.Status == "New" && o.Active == true);
query = query.Where(o => new Point(o.Longitude, o.Latitude)
.IsWithinDistance(new Point(_currentLongitude, _currentLatitude), 50));
var result = query.ToList()
However - it doesn't seem to work and am getting an error as below - any ideas how to solve this? or if there is a better way to get the nearest items?
.Where(p => new Point(p.Longitude, p.Latitude) .IsWithinDistance(geom: __p_3,
distance: ___maxDistance_4))' could not be translated.Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Startup.cs:
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
x => x.UseNetTopologySuite());
});