c# - Expression function: Generic type T predicate -


is possible parse generic type t predicate expression , convert type , create new expression using converted type?

i'm doing conversion t dto.company , after should mapping dto.company dal.company. in below code example i'm trying conversion first need know possible , secondly need know how convert dto.company dal.company inside predicate parameters before setting bar variable.

    // works!!!! want make generic possible!     public t fetch<t>(expression<func<dal.company, bool>> predicate) t : class     {         var typeoft = typeof(t);         if (typeoft != typeof(dto.company))         {             return null;         }                    var companies = _dbcontext.companies.where(predicate);               var ret = new collection<dto.company>();         foreach (var company in companies)         {             ret.add(company.converttodatatransferobject());         }         return (t)convert.changetype(ret, typeof(t));     }      // not work!!!     public t fetch<t>(expression<func<t, bool>> predicate) t : class     {         var typeoft = typeof(t);         if (typeoft != typeof(dto.company))         {             return null;         }         var foo = expression.convert(predicate.body, typeof(bool));       //below i'm getting exception thrown: 'system.argumentexception' in system.core.dll     //additional information: parameterexpression of type 'dto.company' cannot used delegate parameter of type 'dal.company'          var bar = expression.lambda<func<dal.company, bool>>(foo, predicate.parameters);         var companies = _dbcontext.companies.where(bar);          var ret = new collection<dto.company>();         foreach (var company in companies)         {             ret.add(company.converttodatatransferobject());         }         return (t)convert.changetype(ret, typeof(t));     } 

ok, changed logic little bit.

first logic was:

  1. service layer takes dto parameters , returns dto objects.
  2. repository layer takes dto parameters , predicate parameters dto , returns dto pbjects.

now it's:

  1. service layer takes dto parameters , returns dto objects.
  2. repository layer takes dto paramaters , predicate parameters dal , returns dto objects.

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -