I am trying to do get nearest records like below but the sort operation complained to expressions not allowed. How can achieve this in MongoDB?
Error:
NotSupportedException: Only fields are allowed in a $sort. MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateOrderBy(OrderByExpression node)
Code:
public virtual List<Post> PagingNearest(Expression<Func<Post, bool>> predicate, int offset, int limit, GeoCoordinatePortable.GeoCoordinate coord)
{
return _context.Posts.AsQueryable()
.Where(predicate)
.OrderBy(x => x.Location.GeoCoordinate.GetDistanceTo(coord))
.Skip(offset)
.Take(limit)
.ToList();
}