title: Using Dynamic LINQ Where in a one-to-many Entity Framework relationship
body: <p>I am using EF 6 and .net 4.5. 
  I have a "Documents" table with the PK "DocumentId" (Int) and another column, "Title". I have another table called "DocumentFilters" with the columns "DocumentId" (Int) and "DocGUID" (Guid). There is a FK on "Doc Id" between the two tables. It is a one to many Id's relationship between the Documents and DocumentFilters tables. The Model looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/sY81k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sY81k.png" alt="DocModel"></a></p>
  
  <p>I'm trying to write a Dynamic LINQ query using the DynamicLibrary.cs library from the NuGet package.</p>
  
  <p>With regular Linq, the query looks something like this in c#:</p>
  
  <pre><code>DocDBContext db = new DocDBContext();
  var documents = db.Documents.Include(d =&gt; d.DocumentFilters);
  Guid gTest = Guid.Parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
  
  documents = documents.Where(d =&gt; d.DocumentFilters.Select(f =&gt; f.DocGUID).Contains(gTest));
  </code></pre>
  
  <p>This works perfectly and retrieves the documents delimited by the GUID filter that I'm interested in. However, I need to place this within a filter string dynamically, where it may contain other filters. Is it possible to do that? I've tried code like:</p>
  
  <pre><code>documents = documents.Where("DocumentFilters.Select(DocGUID).Contains(@0)", gTest);
  </code></pre>
  
  <p>Error: No applicable aggregate method 'Select' exists</p>
  
  <p>or</p>
  
  <pre><code>documents = documents.Where("DocumentFilters.DocGUID=@0", gTest);
  </code></pre>
  
  <p>Error: GUID property does not exist on DocumentFilters.
  (Which makes sense because DocumentFilters is a collection).</p>
  
  <p>I also thought it might be worth to give this a shot:</p>
  
  <pre><code>documents = documents.Where("DocumentFilters.AsQueryable().Select(DocGUID).Contains(@0)", gTest);
  </code></pre>
  
  <p>But again, it doesn't look like the library supports AsQueryable and throws
  Error: No applicable aggregate method 'AsQueryable' exists.</p>
  
  <p>Is it possible to write such a restriction on a property of an enumerable object within the Documents table?</p>