I have a Linq Expression, which may be altered depending on certain conditions. An example of what I would like to do (left blank the bit I am not sure about):
Expression<Func<Project, bool>> filter = (Project p) => p.UserName == "Bob";
if(showArchived)
{
// update filter to add && p.Archived
}
// query the database when the filter is built
IEnumerable<Project> projects = unitOfWork.ProjectRepository.Get(filter);
How do I update the filter to add any extra parameters?
At the moment all the records are retrieved, then I use a Where
to further filter the results. However that results in more queries to the database than are strictly necessary.
IEnumerable<Project> projects = unitOfWork.ProjectRepository.Get(filter);
if(showArchived)
{
projects = projects.Where(p => p.Archived);
}
Get method is using the GenericRepository pattern:
public class GenericRepository<TEntity> where TEntity : class
{
internal ProgrammeDBContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(ProgrammeDBContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters)
{
return dbSet.SqlQuery(query, parameters).ToList();
}
}
Update
Created some extension methods based on the code below by Marc Gravell and David B, solves the problem for me
public static class LinqExtensionMethods
{
public static Expression<Func<T, bool>> CombineOr<T>(params Expression<Func<T, bool>>[] filters)
{
return filters.CombineOr();
}
public static Expression<Func<T, bool>> CombineOr<T>(this IEnumerable<Expression<Func<T, bool>>> filters)
{
if (!filters.Any())
{
Expression<Func<T, bool>> alwaysTrue = x => true;
return alwaysTrue;
}
Expression<Func<T, bool>> firstFilter = filters.First();
var lastFilter = firstFilter;
Expression<Func<T, bool>> result = null;
foreach (var nextFilter in filters.Skip(1))
{
var nextExpression = new ReplaceVisitor(lastFilter.Parameters[0], nextFilter.Parameters[0]).Visit(lastFilter.Body);
result = Expression.Lambda<Func<T, bool>>(Expression.OrElse(nextExpression, nextFilter.Body), nextFilter.Parameters);
lastFilter = nextFilter;
}
return result;
}
public static Expression<Func<T, bool>> CombineAnd<T>(params Expression<Func<T, bool>>[] filters)
{
return filters.CombineAnd();
}
public static Expression<Func<T, bool>> CombineAnd<T>(this IEnumerable<Expression<Func<T, bool>>> filters)
{
if (!filters.Any())
{
Expression<Func<T, bool>> alwaysTrue = x => true;
return alwaysTrue;
}
Expression<Func<T, bool>> firstFilter = filters.First();
var lastFilter = firstFilter;
Expression<Func<T, bool>> result = null;
foreach (var nextFilter in filters.Skip(1))
{
var nextExpression = new ReplaceVisitor(lastFilter.Parameters[0], nextFilter.Parameters[0]).Visit(lastFilter.Body);
result = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(nextExpression, nextFilter.Body), nextFilter.Parameters);
lastFilter = nextFilter;
}
return result;
}
class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
}