I have a Point created from a lat/lng and stored in my database. Utilizing NetTopologySuite here. SRID 4326.
I can successfully query it for any locations in range of the provided Point.
var search = _context.Services.AsQueryable();
search = search.Where(x => x.ServiceRadius == 0
|| x.Locations.Any(y => y.GeoLocation.Distance(searchPoint) <= x.ServiceRadius));
However, when I add the OrderBy Distance it fails to translate. I swear I've done something similar to this before. Googlefoo is failing me miserably.
search = search.OrderBy(x => x.Locations.Select(y => y.GeoLocation.Distance(searchPoint)));
return search.Select(x => x.Id).ToList();
Which really should work, I would think. However, I get this error:
[wall of code] 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
The basic model setup is Locations that have many Services:
public class LocationModel
{
public Point? GeoLocation { get; set; }
public List<ServiceModel> Services { get; set; } = new();
}
public class ServiceModel
{
public List<LocationModel> Locations { get; set; } = new();
public int ServiceRadius { get; set; } = 0;
}
Again, removing OrderBy line it works, adding it back it fails. I definitely do not want to switch to client evaluation.