gpt4 book ai didi

c# - DocumentDb DocumentClient 获取前 25 个文档

转载 作者:行者123 更新时间:2023-12-03 05:47:24 26 4
gpt4 key购买 nike

Cosmos 入门和文档 db/sql。为什么这不起作用?我可以看到,没有抛出任何错误。有数据应该返回。

    private const string EndpointUri = "some url";
private const string PrimaryKey = "somekey";
private const string DbId = "People";
private const string CollectionId = "Person";
private DocumentClient client;

// GET: api/Person
[HttpGet]
public IEnumerable<Person> Get()
{
this.client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);
FeedOptions queryOptions = new FeedOptions { MaxItemCount = 25, EnableCrossPartitionQuery = true };



IQueryable<Person> personQuery = this.client.CreateDocumentQuery<Person>(
UriFactory.CreateDocumentCollectionUri(DbId, CollectionId), queryOptions)
.Where(f => f.NameFirst != "Andersen");

List<Person> retVal = new List<Person>();
retVal = personQuery.ToList();
return retVal;
}

最佳答案

MaxItemCount 是每次枚举操作将获得的最大元素数。它不会返回前 25 个文档,而是返回每个枚举 25 个文档的聚合批处理中与此查询匹配的所有文档。

如果您想要前 25 项,您的代码应如下所示:

[HttpGet]
public async Task<IEnumerable<Person>> Get()
{
this.client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);
FeedOptions queryOptions = new FeedOptions { EnableCrossPartitionQuery = true };

var personQuery = this.client.CreateDocumentQuery<Person>(
UriFactory.CreateDocumentCollectionUri(DbId, CollectionId), queryOptions)
.Where(f => f.NameFirst != "Andersen").Take(25).AsDocumentQuery();

List<Person> retVal = new List<Person>();

while(personQuery.HasMoreResults)
{
var results = await personQuery.ExecuteNextAsync<Person>();
retVal.AddRange(results);
}

return retVal;
}

根据您在集合中索引字符串的方式,您可能还需要将 FeedOptions 对象的 EnableScanInQuery 属性设置为 true

关于c# - DocumentDb DocumentClient 获取前 25 个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52466121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com