【问题标题】:OrderBy in documentdb linq query in IDocumentQueryIDocumentQuery 中 documentdb linq 查询中的 OrderBy
【发布时间】:2025-11-21 09:10:02
【问题描述】:

我尝试在 c# 中 documentdb 下的 IDocumentQuery 查询中使用 OrderBy。 我尝试按距离我所在位置的距离检索课程列表,因此:

var userQuery = _client
    .CreateDocumentQuery<Course>(_uriCourseCollection, options)
    .Where(x => x.CourseName.ToLower().Contains(text))
    .OrderBy(x =>x.GeoLocation.Location.Distance(myPoint))
    .AsDocumentQuery();

我收到以下错误消息:

"不支持 ORDER BY 子句。无法使用 ORDER BY 项表达式 映射到文档路径"

有人可能有解决方案? ,还是以有效方式解决查询的解决方法?

【问题讨论】:

  • 你引用过这个document吗? Azure Cosmos DB returns an error when a query uses Order By but does not have a range index against the queried path with the maximum precision.Order By queries by default also require maximum index precision (-1). DataType can be String or Number

标签: c# sql linq azure azure-cosmosdb


【解决方案1】:

“不支持 ORDER BY 子句。ORDER BY 项表达式无法映射到文档路径”

根据Azure官方document,目前不支持ST_DISTANCE空间函数排序结果。我也找到了这个feedback,你可以投票。

Order By 查询默认也需要最大索引精度 (-1)。 DataType 可以是 StringNumber

【讨论】: