Q:

How to add extra parameter to Expression> predicate in Generic Repository

I'm using generic repository and I need to add extra parameter in one of the repository methods. For instance I have isDeleted column on database, I want to add this column as false to predicate.
How can I add extra parameter to predicate from repository method? This extra parameter is fixed for all tables. (isDeleted = false)
This is my original method which is getting single record from db.
public T GetSingle(Expression<Func<T, bool>> expression)
{
    return _dbSet.Where(expression).SingleOrDefault();
}

This is updated version to add extra parameter I came so far.
public T GetSingle(Expression<Func<T, bool>> expression)
{
    Expression<Func<T, bool>> extra = d => d.GetType().GetProperty("isDeleted").Equals(false);
    var exp = Expression.And(expression.Body, extra.Body);

    var body = Expression.And(expression.Body, extra.Body);
    var lambda = Expression.Lambda<Func<T, bool>>(body, extra.Parameters);

    return _dbSet.Where(lambda).SingleOrDefault();
}

But this updated version gives lambda body like this and ofcouse it's not working.
((d.ID == value(ProjectName.Namespace.Controllers.ControllerName).User.CompanyId) And d.GetType().GetProperty("isDeleted").Equals(Convert(False)))

A:

public T GetSingle<T>(Expression<Func<T, bool>> expression)
{
    var @params = expression.Parameters;
    var checkNotDeleted = Expression.Equal(Expression.PropertyOrField(@params[0], "isDeleted"), Expression.Constant(false));

    var originalBody = expression.Body;

    var fullExpr = Expression.And(originalBody, checkNotDeleted);

    var lambda = Expression.Lambda<Func<T, bool>>(fullExpr, @params);

    return _dbSet.Where(lambda).SingleOrDefault();
}

Once you start using Expression's methods, you need to always use them (except for Expression.Constant, of course). All logic and code you write must be represented with Expression nodes.
There's a method PropertyOrField which reads the value of a property from a particular expression. In this case, we're reading it from the parameter (that is, d => d.isDeleted - we're writing the d.isDeleted part). Then we need to compare the value to false.
Finally, we simply And the original expression to ours, and create a lambda with the original parameters.

