title: Filter with EntityDatasource in ASP.NET webforms
body: <p>I have two Classes (given via Entity Framework) which are in a many to many relationship:</p>
  
  <pre><code>Corporation which has the member Tags
  
  Tag which has the member name
  </code></pre>
  
  <p>The EntityDataSource gives me the ObjectQuery which i want to filter in a given tagname but i don't know how.  I want to get all corporations, which have a tag with the name "myname". I don't know how to do the linq query</p>
  
  <p>When i query the entities i unfortunately don't get an Objectquery.</p>
  
  <pre><code>protected void EntityDataSource1_QueryCreated(object sender, QueryCreatedEventArgs e)
  {
      // first try
      var corps = e.Query.Cast&lt;Corporation&gt;(); 
      // of course doesn't work, because oyu can't access a member (name) of a collection (Tags) 
      // i don't know the right linq expression for this
      e.Query = from c in corps where c.Tags.Name.Contains("myname") select c; 
  
      // second try
      var tags = from t in entities.Tags where t.Name.Contains("myname") select t;
      var filteredcorporations = from c in tags select c.Corporations;
      // does not work because it is not a ObjectQuery&lt;Corporation&gt;
      e.query = filteredcorporations; 
  }
  </code></pre>
  
  <p>My EntityDataSource:</p>
  
  <pre><code>&lt;asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=eodbEntities" DefaultContainerName="eodbEntities" EnableFlattening="False" EntitySetName="Corporations" OnQueryCreated="EntityDataSource1_QueryCreated"&gt;
  &lt;/asp:EntityDataSource&gt;
  </code></pre>