REST 中的排序结果 (
排序定义返回的记录序列,并支撑稳定的分页。 在 REST 中,数据 API 生成器(DAB)使用 $orderby 查询参数对结果进行排序,然后再应用 $first 或 $after。 如果省略 $orderby,DAB 默认按主键排序(升序)。
注释
对于复合主键,DAB 按数据库列序列排序。
转到 本文档的 GraphQL 版本。
概述
| 概念 | Description |
|---|---|
| 查询参数 | $orderby |
| 方向标记 |
asc、desc |
| 默认顺序 | 主键升序 |
| 多字段顺序 | 逗号分隔字段列表 |
| 平局 | 自动追加的剩余主键字段 |
使用模式
GET /api/{entity}?$orderby=FieldA [asc|desc], FieldB desc
Example
按年份降序订购书籍,然后标题升序。
GET /api/books?$orderby=year desc, title asc&$first=5
概念 SQL
SELECT TOP (5)
id,
sku_title AS title,
year
FROM dbo.books
ORDER BY year DESC, sku_title ASC, id ASC;
示例响应
{
"value": [
{ "id": 7, "title": "Dune Messiah", "year": 1969 },
{ "id": 6, "title": "Dune", "year": 1965 },
{ "id": 3, "title": "Foundation", "year": 1951 },
{ "id": 1, "title": "I, Robot", "year": 1950 },
{ "id": 8, "title": "The Martian Chronicles", "year": 1950 }
]
}
规则
| 规则 | 详细信息 |
|---|---|
| 分隔符 | 逗号 + 可选空格 |
| Direction 关键字 |
asc (默认值), desc |
| 事例敏感性 | 字段名称区分大小写 |
| 未知字段 | 返回 400 Invalid orderby column requested |
| 不支持的语法 | 返回 400 OrderBy property is not supported. |
示例错误
无效的字段名称:
400 Invalid orderby column requested: publishedYear
不支持的令牌:
400 OrderBy property is not supported.
与 $select
按不存在的 $select字段进行排序时,DAB 会从内部提取它进行排序或游标创建,但从最终有效负载中省略它。
相关配置
{
"entities": {
"Book": {
"source": {
"object": "dbo.books",
"type": "table"
},
"mappings": {
"sku_title": "title"
}
}
}
}
示例配置
{
"runtime": {
"pagination": {
"default-page-size": 100,
"max-page-size": 100000
}
},
"entities": {
"Book": {
"source": {
"type": "table",
"object": "dbo.books"
},
"mappings": {
"sku_title": "title",
"sku_price": "price"
},
"relationships": {
"book_category": {
"cardinality": "one",
"target.entity": "Category",
"source.fields": [ "category_id" ],
"target.fields": [ "id" ]
}
}
},
"Category": {
"source": {
"type": "table",
"object": "dbo.categories"
},
"relationships": {
"category_books": {
"cardinality": "many",
"target.entity": "Book",
"source.fields": [ "id" ],
"target.fields": [ "category_id" ]
}
}
}
}
}
另请参阅
| 概念 | REST | GraphQL | 目的 |
|---|---|---|---|
| 投影 | $select | 物品 | 选择要返回的字段 |
| Filtering | $filter | 滤波器 | 按条件限制行 |
| 排序 | $orderby | orderBy | 定义排序顺序 |
| 页面大小 | $first | first | 限制每页的项数 |
| 延续 | $after | 后 | 使用光标从最后一页继续 |
注释
REST 关键字以 $遵循 OData 约定开头。