title: DocumentDB LINQ match multiple child objects
body: <p>I have a simple document.</p>
  
  <pre><code>{
      Name: "Foo",
      Tags: [
          { Name: "Type", Value: "One" },
          { Name: "Category", Value: "A" },
          { Name: "Source", Value: "Example" },
      ]
  }
  </code></pre>
  
  <p>I would like to make a LINQ query that can find these documents by matching multiple <code>Tags</code>.</p>
  
  <p>i.e. Not a SQL query, unless there is no other option.</p>
  
  <p>e.g.</p>
  
  <pre><code>var tagsToMatch = new List&lt;Tag&gt;()
  {
      new Tag("Type", "One"),
      new Tag("Category", "A")
  };
  
  var query = client
      .CreateDocumentQuery&lt;T&gt;(documentCollectionUri)
      .Where(d =&gt; tagsToMatch.All(tagToMatch =&gt; d.Tags.Any(tag =&gt; tag == tagToMatch)));
  </code></pre>
  
  <p>Which gives me the error <code>Method 'All' is not supported.</code>.</p>
  
  <p>I have found examples where a single property on the child object is being matched: <a href="https://stackoverflow.com/questions/38275668/linq-query-issue-with-using-any-on-documentdb-for-child-collection">LINQ Query Issue with using Any on DocumentDB for child collection</a></p>
  
  <pre><code>var singleTagToMatch = tagsToMatch.First();
  
  var query = client
      .CreateDocumentQuery&lt;T&gt;(documentCollectionUri)
      .SelectMany
      (
          d =&gt; d.Tags
              .Where(t =&gt; t.Name == singleTagToMatch.Name &amp;&amp; t.Value == singleTagToMatch.Value)
              .Select(t =&gt; d)
      );
  </code></pre>
  
  <p>But it's not obvious how that approach can be extended to support matching multiple child objects.</p>
  
  <p>I found there's a function called ARRAY_CONTAINS which can be used: <a href="https://stackoverflow.com/questions/40683533/azure-documentdb-array-contains-on-nested-documents">Azure DocumentDB ARRAY_CONTAINS on nested documents</a></p>
  
  <p>But all the examples I came across are using SQL queries.</p>
  
  <p>This <a href="https://social.msdn.microsoft.com/Forums/windowsserver/en-US/a951a271-e729-474b-bf04-ed4d413cb096/arraycontains-using-in-linq?forum=AzureDocumentDB" rel="nofollow noreferrer">thread</a> indicates that LINQ support was "coming soon" in 2015, but it was never followed up so I assume it wasn't added.</p>
  
  <p>I haven't come across any documentation for <code>ARRAY_CONTAINS</code> in LINQ, only in <a href="https://docs.microsoft.com/en-us/azure/cosmos-db/documentdb-sql-query-reference#bk_array_contains" rel="nofollow noreferrer">SQL</a>.</p>
  
  <p><s>
  I tried the following SQL query to see if it does what I want, and it didn't return any results:</p>
  
  <pre><code>SELECT  Document
  FROM    Document
  WHERE   ARRAY_CONTAINS(Document.Tags, { Name: "Type", Value: "One" })
  AND     ARRAY_CONTAINS(Document.Tags, { Name: "Category", Value: "A" })
  </code></pre>
  
  <p>According to the <a href="https://stackoverflow.com/a/35139132/310988">comments on this answer</a>, <code>ARRAY_CONTAINS</code> only works on arrays of primitives, not objects. SO it appears not to be suited for what I want to achieve.
  </s></p>
  
  <p>It seems the comments on that answer are wrong, and I had syntax errors in my query. I needed to add double quotes around the property names.</p>
  
  <p>Running this query did return the results I wanted:</p>
  
  <pre><code>SELECT  Document
  FROM    Document
  WHERE   ARRAY_CONTAINS(Document.Tags, { "Name": "Type", "Value": "One" })
  AND     ARRAY_CONTAINS(Document.Tags, { "Name": "Category", "Value": "A" })
  </code></pre>
  
  <p>So <code>ARRAY_CONTAINS</code> does appear to achieve what I want, so I'm looking for how to use it via the LINQ syntax. </p>