I have a generic method to query objects of type TEntity in EF. I Want to add a condition as a where clause if TEntity implements a specific interface. The method I have is:
This is a working solution:
public TEntity GetByUserID(Guid userID, params Include[] includes)
{
var query = this.DbSet;
query = Where(query, x => !x.IsDeleted);
return query
.FirstOrDefault(x => x.UserID == userID);
}
public static IQueryable Where(IQueryable query, Expression> predicate)
{
if (typeof(TEntity).IsImplementationOf())
{
query = ((IQueryable)query)
.Where(predicate)
.Cast();
}
return query;
}