1

Vote my question to be closed

I have found a similar question and a really useful answer using ExpressionVisitor class (Link: How can I convert a lambda-expression between different (but compatible) models?). Thank you all, I'm voting to my answer become closed as duplicate, please consider voting too.

Code

I'm developing a repository code that uses a Data Transfer Object, like the code below.

public class UsuarioRepositorio : IUsuarioRepository
{
    private readonly MongoRepository<UsuarioDto> _Repository;

    public UsuarioRepository(string connectionString)
    {
        _Repositorio = new MongoRepository<UsuarioDto>(connectionString, "");
    }
}

public interface IUsuarioRepository
{  
    IEnumerable<T> Select(Expression<Func<Usuario, bool>> predicate);
}
  • UsuarioDto is the data transfer object for the Usuario class, both inheriting from the interface IUsuario.

  • The UsuarioRepository implements the IUsuarioRepository interface, and has a private member called _Repository, which belongs to the MongoRepository<UsuarioDto> type.

  • The _Repository member has a method called Select which accepts an argument of type Expression<Func<UsuarioDto, bool>>.

  • The IUsuarioRepository has a declared method called Select which accepts an argument of type Expression<Func<Usuario, bool>>.


Problem

The problem is that I need to implement the Select method in UsuarioRepository, using the IUsuarioRepository method signature and passing to _Repository a new expression of Expression<Func<UsuarioDto, bool>> type, with the same parameters of Expression<Func<Usuario, bool>> argument.

Basically I need a way to copy the expression parameters to a new expression of different type, knowing that the expressions has the same properties because they have the same interface inheritance. Something like this:

public IEnumerable<Usuario> Select(Expression<Func<Usuario, bool>> predicate)
{
    Expression<Func<UsuarioDto, bool>> transferExpression = x => x != null;
    transferExpression = transferExpression .Update(predicate.Body, predicate.Parameters);

    return _Repository.Select(transferExpression ).ToList().Select(x => x.ToDomain());
}


Questions

  • The Update method of Expression type does work like the code above?
  • If it does not work, is there a way to copy expressions of different types, but with the same base/interface properties?

Thank you very much!

2

1 Answer 1

0

I have a blog post about combining 2 expressions of the same type here: http://blog.waseem-sabjee.com/2013/07/23/linq-expression-how-to-append-to-an-expression-at-a-later-stage/

all you will need to do to achieve working with 2 types is alter the static methods in my extension class LambdaExtensions to work with T, T2 instead of T

a word of warning, if referencing a property of T2 that is not in T1, it may not be successful - you will need to handle this.

I've provided you with a starting point, and I will also be attempting this myself, I will update this answer later - but feel free to try it out yourself so long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.