title: Entity Framework type case generic in predicate
body: <p>I am working on updating to a more manageable repository pattern in my MVC 4 project that uses Entity Framework code first. I've integrated a generic base repository class that will do basic CRUD operations so I don't have to implement these in each repository I create. I have ran into an issue where my All method needs to filter there query by a deleted flag if the entity is a type of TrackableEntity. Since the Entity is generic in the base repository I am attempting to cast is to a type of TrackableEntity in the where which just results in the following error message.</p>
  
  <blockquote>
    <p>The 'TypeAs' expression with an input of type 'NameSpace.Models.ClientFormField' and a check of type 'NameSpace.Models.TrackableEntity' is not supported. Only entity types and complex types are supported in LINQ to Entities queries.</p>
  </blockquote>
  
  <p>This error makes complete since and I understand why the code I have is not working but I am trying to find a way to filter out deleted items without having to override this method in all of my repositories. The code I have for my All method is below.</p>
  
  <pre><code>public virtual IEnumerable&lt;T&gt; All()
  {
      if (typeof(T).IsSubclassOf(typeof(TrackableEntity)))
          return dbSet.Where(e =&gt; !(e as TrackableEntity).IsDeleted).ToList();
  
      return dbSet.ToList();
  }
  </code></pre>
  
  <p>I know that I can do the following</p>
  
  <pre><code>public virtual IEnumerable&lt;T&gt; All(Expression&lt;Func&lt;T, bool&gt;&gt; predicate = null)
  {
      if (predicate != null)
          return dbSet.Where(predicate).IsDeleted).ToList();
  
      return dbSet.ToList();
  }
  </code></pre>
  
  <p>And then add this to all of my repositories</p>
  
  <pre><code>public override IEnumerable&lt;CaseType&gt; All(Expression&lt;Func&lt;CaseType,bool&gt;&gt; predicate = null)
  {
      if (predicate == null)
          predicate = e =&gt; !e.IsDeleted;
      return base.All(predicate);
  }
  </code></pre>
  
  <p>The problem I have with this is that I am duplicating code, this is basically a copy and paste into all of my repositories which defeats the purpose of changing to this new repository pattern. I made the switch to end duplicated code in my repositories.</p>
  
  <p>Here is an example of one of my entities.</p>
  
  <pre><code>public class CaseType : TrackableEntity, IValidatableObject
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public bool InUse { get; set; }
  
      public bool IsValid { get { return !this.Validate(null).Any(); } }
  
      public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
      {
          if (String.IsNullOrEmpty(Name))
              yield return new ValidationResult("Case Type name cannot be blank", new[] { "Name" });
  
          //Finish Validation Rules
      }
  }
  </code></pre>
  
  <p>And the TrackableEntity</p>
  
  <pre><code>public abstract class TrackableEntity
  {
      public bool Active { get; set; }
      public bool IsDeleted { get; set; }
      public virtual User CreatedBy { get; set; }
      public virtual User ModifiedBy { get; set; }
      public DateTime DateCreated { get; set; }
      public DateTime DateModified { get; set; }
  }
  </code></pre>
  
  <p>Any help on this would be much appreciated.</p>