我ProductDocument
在 CosmosDB 中有一个模型,它代表一个产品。在该模型中有一个子文档contributors
,其中包含对产品做出贡献的人。每个贡献者都有一个role
.
现在我一直在试验一个需要:
- 只选择
ProductDocument
一个contributor.roleDescription
作者 - 仅
ProductDocument
与Pub 1division
中的一个一起选择 - 只在结果集中包含
contributors
带有Authorcontributor.roleDescription
的子文档。
现在我正在努力:
- 上面选择的第 3 部分。我如何完成这一点,因为我的结果集包括
contributor.roleDescription
作者和插画家
示例 Cosmos 模型:
[
{
"id": "1",
"coverTitle": "A Title",
"pubPrice": 2.99,
"division" :"Pub 1",
"Availability": {
"code": "20",
"description": "Digital No Stock"
},
"contributors": [
{
"id": 1,
"firstName": "Brad",
"lastName": "Smith",
"roleDescription": "Author",
"roleCode": "A01"
},
{
"id": 2,
"firstName": "Steve",
"lastName": "Bradley",
"roleDescription": "Illustrator",
"roleCode": "A12"
}
]
},
{
"id": "2",
"coverTitle": "Another Title",
"division" :"Pub 2",
"pubPrice": 2.99,
"Availability": {
"code": "50",
"description": "In Stock"
},
"contributors": [
{
"id": 1,
"firstName": "Gareth Bradley",
"lastName": "Smith",
"roleDescription": "Author",
"roleCode": "A01"
}
]
}]
这是我在Data Explorer中使用的 SQL :
SELECT VALUE p
FROM Products p
JOIN c IN p.contributors
WHERE c.roleDescription = 'Author'
AND p.division = 'Pub 1'
这是我的服务中的 LINQ 查询:
var query = client.CreateDocumentQuery<ProductDocument>(
UriFactory.CreateDocumentCollectionUri("BiblioAPI", "Products"),
new FeedOptions
{
MaxItemCount = -1,
EnableCrossPartitionQuery = true
}
)
.SelectMany(product => product.Contributors
.Where(contributor => contributor.RoleDescription == "Author")
.Select(c => product)
.Where(p => product.Division == "Pub 1"))
.AsDocumentQuery();
List<ProductDocument> results = new List<ProductDocument>();
while (query.HasMoreResults)
{
results.AddRange(await query.ExecuteNextAsync<ProductDocument>());
}
它选择了正确的记录,但是我如何取消选择贡献者的Illustrator子文档,因为目前我得到以下信息:
{
"id": "1",
"coverTitle": "A Title",
"pubPrice": 2.99,
"division" :"Pub 1",
"Availability": {
"code": "20",
"description": "Digital No Stock"
},
"contributors": [
{
"id": 1,
"firstName": "Brad",
"lastName": "Smith",
"roleDescription": "Author",
"roleCode": "A01"
},
{
"id": 2,
"firstName": "Steve",
"lastName": "Bradley",
"roleDescription": "Illustrator",
"roleCode": "A12"
}
]
}
但以下输出是我想要的,不包括 Illustrator 贡献者子文档:
{
"id": "1",
"coverTitle": "A Title",
"pubPrice": 2.99,
"division" :"Pub 1",
"Availability": {
"code": "20",
"description": "Digital No Stock"
},
"contributors": [
{
"id": 1,
"firstName": "Brad",
"lastName": "Smith",
"roleDescription": "Author",
"roleCode": "A01"
}
]
}
编辑:
我想过滤
Product
子文档之一是否contributor.roleDescription
等于作者。因此,如果产品记录不包括作者贡献者,我不想要它我想包括每个
contributor
等于Author的子文档。因此,如果 a 有多个作者贡献者子文档Product
,我想包括它们,但排除Illustrator的。你可以有一个
ProductDocuments
.对流利的 LINQ 语法的帮助将有很大帮助。