c# - Convert Func<TDao, bool> to Func<TEntity, bool> -
given:
class mydao { public int siteid {get;set;} public cv3addressdao readsingle(expression<func<cv3addressdao, bool>> predicate) { //... } } class myentity { public int siteid {get;set;} } how can take predicate parameter of type expression<func<mydao, bool>> , convert expression<func<myentity, bool>>?
the type of answer looking for
please note answers must show how conversion works. want implementation similar works :)....
public mydao readsingle(expression<func<mydao , bool>> predicate) { var mappedpredicate = mapfun<mydao , myentity>(predicate); var result = repository.getsingle<myentity>(mappedpredicate); return convert(result);//converts entity dao...safe ignore line } expression<func<b, bool>> mapfun<a, b>(expression<func<a, bool>> input) { expression<func<b, bool>> result = null;//how convert? return result; } details regarding why want design...
this asp.net mvc 5 application. reason conversion important view not aware of type myentity. in other words if do:
mydaoobject.readsingle<myentity>(myentity => myentity.siteid == "123");
then view layer has reference dll layer because here using myentity. want view layer work dao instead:
mydaoobject.readsingle<mydao>(mydao=> mydao.siteid == "123");
but, have convert dao make useable repository. repository aware of dl objects. trying avoid creating redundant repository support translation. dao contains properties of entity plus some.
i don't have exact code handy, want consume expression provided , perform equivalency conversion yourself.
this stackoverflow question along lines of do.
note p.brian.mackey
the referred answer worked. using jon's answer trick. however, public portion of answer ended being non-visible player func<> conversion. needed was:
expression<func<b, bool>> mapfun<a, b>(expression<func<a, bool>> input)where : class { var result = dbaccesslayer.transformpredicatelambda<a,b>(input); return result; } note method private. changed public. see @jon answer in answer referenced above full implementation.
public static expression<func<tnewtarget, bool>> transformpredicatelambda<toldtarget, tnewtarget>(expression<func<toldtarget, bool>> predicate)
Comments
Post a Comment