title: Entity Framework: Query Multiple Types
body: <p>I have a EF6 code-first model with inheritance, for example:</p>
  
  <pre><code>[Table("Things")]
  abstract class AbstractThing
  {
     public int ThingId { get; set; }
     public string Color { get; set; }
  }
  
  class Car : AbstractThing
  {
     public string Brand { get; set; }
  }
  
  class Balloon : AbstractThing
  {
     public double Price { get; set; }
  }
  
  class Animal : AbstractThing
  {
     public int LegCount { get; set; }
  }
  </code></pre>
  
  <p>I use table-per-hierarchy, so my "Things" table will have the columns:</p>
  
  <ul>
  <li>ThingId : int</li>
  <li>Color : string</li>
  <li>Brand : string</li>
  <li>Price : double</li>
  <li>LegCount : int</li>
  <li>Discriminator : string</li>
  </ul>
  
  <p>Now, I want to query for things that are of specific types, but this list of types is dynamic. E.g. I want all red things that are Cars or Ballons. In SQL that is quite easily done, as I could just compile a <code>where Discriminator in ('Car', 'Ballon')</code> clause.</p>
  
  <p>The problem here is that, to my knowledge, the only way to filter by type in EF Linq2SQL is by using the .OfType method (or use specific collections). But then I get back the filtered list that only contains that type, I cannot use an 'OR' or 'IN' clause. I can't really figure out how to do this without explicitly querying every set and making a union:</p>
  
  <pre><code>var queryable = Enumerable.Empty&lt;Thing&gt;().AsQueryable();
  
  if (typesRequested.Contains(typeof(Car)))
  {
     queryable = queryable.Union(context.Things.OfType&lt;Car&gt;());
  }
  if (typesRequested.Contains(typeof(Balloon)))
  {
     queryable = queryable.Union(context.Things.OfType&lt;Balloon&gt;());
  }
  if (typesRequested.Contains(typeof(Animal)))
  {
     queryable = queryable.Union(context.Things.OfType&lt;Animal&gt;());
  }
  </code></pre>
  
  <p>As in my actual model I have a lot more inherited types, this approach is not so feasible. The workaround I have in place now is to have a custom 'type' column that is redundant with the 'Discriminator' column, but this is error prone and creates boiler plate code.</p>
  
  <p>Is there any other way to dynamically restrict a queryable to return only a specific set of types?</p>