title: C# MongoDB Driver - How to filter by arrays and a list of items
body: <p>I want to perform a search on an array of strings and compare it to any string in a list of strings. For example:</p>
  
  <p>This is the data structure:</p>
  
  <pre><code>{
      "CategoryIds": [ "A123", "B456", "C789" ]
  }
  </code></pre>
  
  <p>and this is the search term:</p>
  
  <pre><code>List&lt;string&gt; search = new List&lt;string&gt; { "A123", "C789" };
  </code></pre>
  
  <p>I'm building my search query dynamically so I have these defined for start:</p>
  
  <pre><code>var builder = Builders&lt;Item&gt;.Filter;
  ar filters = Builders&lt;Item&gt;.Filter.Empty;
  </code></pre>
  
  <p>I've tried all of the following approaches but none worked:</p>
  
  <p>Using <code>ElemMatch</code></p>
  
  <pre><code>filters &amp;= builder.ElemMatch(i =&gt; i.CategoryIds, id =&gt; search.CategoryIds.Any(i =&gt; i.Equals(id)));
  </code></pre>
  
  <p>Using <code>Intersect</code></p>
  
  <pre><code>filters &amp;= builder.Where(i =&gt; search.CategoryIds.Intersect(i.CategoryIds).Any());
  </code></pre>
  
  <p>The only one that works is this one:</p>
  
  <pre><code>var categoryFilters = new List&lt;FilterDefinition&lt;Item&gt;&gt;();
  
  foreach (string id in search.CategoryIds)
      categoryFilters.Add(builder.AnyEq(i =&gt; i.CategoryIds, id));
  
  if (categoryFilters.Any())
      filters &amp;= builder.Or(categoryFilters);
  </code></pre>
  
  <p>But it seems like overkill. Is there a simpler way to do that?</p>