Azure DocumentDb中数组上的复杂WHERE子句

时间:2015-08-12 17:34:45

标签: linq azure azure-cosmosdb

我有两个看起来像这样的文件。

{      
  "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_CONTAINS,因为我需要利用索引,因为该集合包含超过500,000个文档。

由于

2 个答案:

答案 0 :(得分:2)

您可以编写一个看起来像这样的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")

答案 1 :(得分:0)

不要使用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);