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

时间:2017-09-12 07:04:34

标签: c# asp.net azure-cosmosdb

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

List<SearchInput> posts = queryable.ToList();

它在代码行{{1}}

上显示以下错误
  

{&#34;需要交叉分区查询但已禁用。请设置   x-ms-documentdb-query-enablecrosspartition为true,指定   x-ms-documentdb-partitionkey,或修改您的查询以避免这种情况   例外。\ r \ nActivityId:xxxxxx-xxxx-xxx-xxx-xxxxxxx&#34;}

请帮帮我...

1 个答案:

答案 0 :(得分:25)

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

请关注https://msdn.microsoft.com/library/en-us/Dn850285.aspx链接 对于REST https://docs.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");