最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

entity framework core - Order by distance between stored Point and provided Point - Stack Overflow

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论