title: .NET LINQ to Entities base where clause for generic Type
body: <p>I am using Entity Framework to access my domain models, all of which implement an interface (IPublishable) that specifies properties to indicate whether an item has been published.</p>
  
  <p>When accessing items from the front end I always want to filter out unpublished items. So, every time I write a query I'm doing something similar to this (where the following DbSets Objects1 and Objects2 both implement <code>IPublishable</code></p>
  
  <pre><code>context.Objects1.Where( x =&gt; x.IsPublished ...).OrderBy( x =&gt; x.Id).ToList()
  context.Objects2.Where( x =&gt; x.IsPublished ...).First()
  </code></pre>
  
  <p>What would be ideal is if I could inject the common <code>Where()</code> clause into all my queries within my web app OR if there was a way to write an extension method which I could include in each query.</p>
  
  <p>I tried to create the extension method for all linq queries</p>
  
  <pre><code>public static IEnumerable&lt;T&gt; FrontEnd&lt;T&gt;(this DbSet&lt;T&gt; dbSet) where T : class {
    return dbSet.Cast&lt;IPublishable&gt;().Where( x =&gt; x.IsPublished ...).Cast&lt;T&gt;();
  }
  </code></pre>
  
  <p>And use like this...</p>
  
  <pre><code>context.Objects1.FrontEnd().Where( x =&gt; ...).OrderBy(...
  </code></pre>
  
  <p>However I get the error "LINQ to Entities only supports casting EDM primitive or enumeration types"</p>
  
  <p>I'm new to EF, so any information would be a great help.
  Thanks</p>