c# - Create a LinQ Expression with another Expression's Parameters -


vote question closed

i have found similar question , useful answer using expressionvisitor class (link: how can convert lambda-expression between different (but compatible) models?). thank all, i'm voting answer become closed duplicate, please consider voting too.

code

i'm developing repository code uses data transfer object, 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 data transfer object usuario class, both inheriting interface iusuario.

  • the usuariorepository implements iusuariorepository interface, , has private member called _repository, belongs mongorepository<usuariodto> type.

  • the _repository member has method called select accepts argument of type expression<func<usuariodto, bool>>.

  • the iusuariorepository has declared method called select accepts argument of type expression<func<usuario, bool>>.


problem

the problem need implement select method in usuariorepository, using iusuariorepository method signature , passing _repository new expression of expression<func<usuariodto, bool>> type, same parameters of expression<func<usuario, bool>> argument.

basically need way copy expression parameters new expression of different type, knowing expressions has same properties because have same interface inheritance. 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 work code above?
  • if not work, there way copy expressions of different types, same base/interface properties?

thank much!

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

all need achieve working 2 types alter static methods in extension class lambdaextensions work t, t2 instead of t

a word of warning, if referencing property of t2 not in t1, may not successful - need handle this.

i've provided starting point, , attempting myself, update answer later - feel free try out long.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -