Wednesday, 15 April 2015

C# AutoMapper Conditional Mapping based upon target value -



C# AutoMapper Conditional Mapping based upon target value -

please can 1 advise how utilize conditional mapping in automapper map value in target object source object based upon existing target property value?

so source class is:

public class userdetails { public string nickname { get; set; } }

my target class is:

public class profileviewmodel { public boolean nicknameisvisible { get; set; public string nickname { get; set; } }

i want set "nickname" property value in target match "nickname" property value in source if target property "nicknameisvisible" value set true, otherwise want set target "nickname" property value empty string.

i trying (which wont compile )...

mapper.createmap<userdetails, profileviewmodel>() .formember( destination => destination.nickname, alternative => option. .mapfrom( source => source.nicknameisvisible ? source.nickname : string.empty) );

but "nicknameisvisible" not property of source of target.

btw, profileviewmodel bound 3 entities using owain wragg's method (http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx) , entity gives value "nicknameisvisible" property.

please can suggest right syntax utilize problem?

try this:

mapper.createmap<userdetails, profileviewmodel>() .formember( destination => destination.nickname, alternative => { option.condition(rc => { var profileviewmodel = (profileviewmodel)rc.instancecache.first().value; homecoming profileviewmodel.nicknameisvisible; }); option.mapfrom(source => source.nickname); } );

c# mapping conditional automapper

No comments:

Post a Comment