Microsoft.Azure.Cosmos 带有延续标记的 SDK 分页查询

Microsoft.Azure.Cosmos SDK Paginated Queries with Continuation Token

我正在从 Microsoft.Azure.Documents SDK 迁移到新的 Microsoft.Azure.Cosmos (v3.2.0),但在为分页查询取回延续令牌时遇到问题。在之前的 SDK 中,当您拥有 FeedResponse 对象时,它 return 为 HasMoreResults 和 ContinuationToken 编辑了一个布尔值,如果他们想要调用下一页(通过 API端点)。在新的 SDK 中,我试图在我的容器上使用 GetItemQueryIterator 方法,我看到的唯一示例是使用 while 循环来获取所有使用 HasMoreResults 值的页面,而我无法提取 ContinuationToken 并只传回第一个结果集。

到目前为止我的代码是这样的:

var feedIterator = _documentContext.Container.GetItemQueryIterator<MyDocumentModel>(query, request.ContinuationToken, options);

if (feedIterator.HasMoreResults)
{
    listViewModel.HasMoreResults = true;
    //listViewModel.ContinuationToken = feedIterator.ContinuationToken; (DOES NOT EXIST!)
}

注释掉的行是我通常希望从中提取 ContinuationToken 的地方,但它不存在。

大多数示例都像这样使用迭代器:

while (feedIterator.HasMoreResults)
{
    listViewModel.MyModels.AddRange(_mapper.Map<List<MyModelListViewItem>>(await results.ReadNextAsync()));
}

但我只想 return 单页结果,如果我想获取下一页,则传入一个延续标记。

我没有尝试过代码,但查看了文档,ReadNextAsync() FeedIterator returns you an object of type FeedResponse and that has a ContinuationToken 属性 上的方法。

ContinuationToken 是 ReadNextAsync 响应的一部分:

FeedResponse<MyDocumentModel> response = await feedIterator.ReadNextAsync();
var continuation = response.ContinuationToken;

原因是 ReadNextAsync 是进行服务调用的时刻,代表一页数据,并且继续针对该特定页面。