Linq Azure DocumentDb中数组上的复杂WHERE子句
我有两份像这样的文件Linq Azure DocumentDb中数组上的复杂WHERE子句,linq,azure,azure-cosmosdb,Linq,Azure,Azure Cosmosdb,我有两份像这样的文件 { "CustomFields": [ { "CustomFieldId": "FirstName", "StringValue": "John", }, { "CustomFieldId": "LastName", "StringValue": "Johnson", } ], "id": "f600bd8b-bca8-
{
"CustomFields": [
{
"CustomFieldId": "FirstName",
"StringValue": "John",
},
{
"CustomFieldId": "LastName",
"StringValue": "Johnson",
}
],
"id": "f600bd8b-bca8-41a5-9f1c-0038e9cc7b35",
},
{
"CustomFields": [
{
"CustomFieldId": "FirstName",
"StringValue": "John",
},
{
"CustomFieldId": "LastName",
"StringValue": "Williams",
}
],
"id": "f600bd8b-bca8-41a5-9f1c-0038e9cc7b35",
}
我试过这个:
items = items.Where(x => x.CustomFields.All(
cf =>
cf.CustomFieldId == "01d1beab-8651-41df-ad93-ecc6195e912f" && cf.StringValue == "Pending"));
我想构建一个查询(在SQL或LINQ中-只要它的可执行文件由documentdb执行)来检索所有文档
where (CustomFieldId == "FirstName" and StringValue == "John")
AND (CustomFieldId == "LastName" and StringValue == "Williams")
请不要建议使用ARRAY\u CONTAINS
,因为我需要利用索引,因为集合包含超过500000个文档
谢谢不要使用
All
,如果每个元素都满足您的条件,则只会返回布尔值true
items = items.Where(x =>
x.CustomFields.Where(cf => cf.CustomFieldId == "FirstName" && cf.StringValue == "John").Count() > 0
&& x.CustomFields.Where(cf.CustomFieldId == "LastName" && cf.StringValue == "Williams").Count() > 0);
您可以编写如下所示的SQL查询-
SELECT d
FROM docs d
JOIN f1 IN d.CustomFields
JOIN f2 IN d.CustomFields
WHERE (f1.CustomFieldId = "FirstName" and f1.StringValue == "John")
AND (f2.CustomFieldId == "LastName" and f2.StringValue == "Williams")
但我不想选择自定义字段,我需要父字段。。。。也不是或(| |)如果每个元素都满足您的条件,则它必须同时满足这两个条件。我真的想要这个。。。。问题是它不受支持,
Where
子句中的|
在本例中实际上是AND运算符,因为它将为您提供您所要求的两种内容。如果像这样放置&&
,将永远不会得到任何结果,因为cf.CustomFieldId
永远不能同时等于“FirstName”
和“LastName”
。如果每个元素都满足您的条件。所以你真的想要这个?若要返回布尔值,而不是相应对象的列表,documentdb也不支持计数:(这非常有效…唯一的负面部分是不同条件下的连接数…性能可能会有问题here@ryan-crawcour msft I最多有10种不同的条件(阵列中的项目)这将与之进行比较。这也意味着10种可能的联接,如果对我拥有500000多个文档的集合有性能影响的话?请注意,DocumentDb将联接的数量限制为两个,因此这是正确的答案,但不能满足我的最终需要。从积极的方面来看,查询的性能非常好。