C# $sort mongodb中只允许字段

C# $sort mongodb中只允许字段,c#,mongodb,asp.net-core-2.0,mongodb-.net-driver,C#,Mongodb,Asp.net Core 2.0,Mongodb .net Driver,我正试图像下面这样获取最近的记录,但排序操作向表达式抱怨不允许。如何在MongoDB中实现这一点 错误: NotSupportedException:在$sort中只允许字段。 MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateOrderBy(OrderByExpression 节点) 代码: 公共虚拟列表分页最近(表达式谓词、整数偏移量、整数限制、GeoCoordinatePortable.GeoCoordinate Co

我正试图像下面这样获取最近的记录,但排序操作向表达式抱怨不允许。如何在MongoDB中实现这一点

错误: NotSupportedException:在$sort中只允许字段。 MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateOrderBy(OrderByExpression 节点)

代码:
公共虚拟列表分页最近(表达式谓词、整数偏移量、整数限制、GeoCoordinatePortable.GeoCoordinate Coordinate)
{
return_context.Posts.AsQueryable()
.Where(谓词)
.OrderBy(x=>x.Location.geocordinate.GetDistanceTo(coord))
.跳过(偏移)
.接受(限制)
.ToList();
}

我解决了这个问题,将其强制转换为一个列表,然后进行排序,在我的例子中,再次将返回的IOrderedEnumerable强制转换为一个列表

最后一个case.ToList()当然是可选的,它正好适合我的需要

在我的例子中,我也通过一个布尔值和一个三元运算符进行排序


.ToList().OrderBy(e=>e.SomeBoolean?0:1).ToList()

是否尝试查询包含GeoJSON坐标的多个文档,并根据与特定点的距离对它们进行排序?
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();
}