c# - .NET - c# - 需要跨分区查询,但禁用了 DocumentDB 数据访问问题

标签 c# asp.net azure-cosmosdb

我编写了以下代码来从 DocumentDB 中获取记录

private static void QueryDocuments1(DocumentClient client)
{

    IQueryable<SearchInput> queryable =
client.CreateDocumentQuery<SearchInput>(UriFactory.CreateDocumentCollectionUri(DocumentDBName, DocumentDBCollectionName))
        .Where(x => x.Receiver == "8907180");
    List<SearchInput> posts = queryable.ToList();
}

它在代码行 List<SearchInput> posts = queryable.ToList(); 上显示以下错误

{"Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception.\r\nActivityId: xxxxxx-xxxx-xxx-xxx-xxxxxxx"}

请帮助我...

最佳答案

您应该使用 CreateDocumentQuery 方法并将 FeedOptions 对象作为参数,此类具有 x-ms-documentdb-query-enablecrosspartition 的属性> 调用了 EnableCrossPartitionQuery

请点击链接 https://msdn.microsoft.com/library/en-us/Dn850285.aspx 对于休息 https://learn.microsoft.com/en-us/rest/api/documentdb/querying-documentdb-resources-using-the-rest-api

示例:

你应该有

 var option = new FeedOptions { EnableCrossPartitionQuery = true };
 IQueryable<SearchInput> queryable = client.CreateDocumentQuery<SearchInput>
 (UriFactory.CreateDocumentCollectionUri(DocumentDBName, 
 DocumentDBCollectionName), option ) .Where(x => x.Receiver == "8907180");

关于c# - .NET - c# - 需要跨分区查询,但禁用了 DocumentDB 数据访问问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170004/

相关文章:

c# - .net web api 服务器中的静态变量

c# - Cosmos DB Azure 表 API o数据身份验证 REST/C#?

c# - Azure Cosmos DB - 可以创建 JSON 文档的副本

仅 Azure Cosmos 索引 "id"

c# - DocX c# 库在一行中合并表字段

c# - 如何向 GridView 添加新行

c# - 如何从 C# 转换为 Java JDBC

c# - 如何在 System.Text.RegularExpressions.Regex 中查找整数或小数

asp.net - 将 BoundField 提取到标签

c# - 无效路由的 URL 转发?