c# - How to configure Conditional Mapping in AutoMapper? -


suppose have following entities (classes)

public class target {     public string value; }   public class source {     public string value1;     public string value2; } 

now want configure auto map, map value1 value if value1 starts "a", otherwise want map value2 value.

this have far:

mapper     .createmap<source,target>()     .formember(t => t.value,          o =>              {                 o.condition(s =>                      s.value1.startswith("a"));                 o.mapfrom(s => s.value1);                   <<***but how supply negative clause!?***>>             }) 

however part still eludes me how tell automapper go take s.value2 should earlier condition fails.

it seems me api not designed be... may it's lack of knowledge getting in way.

please help.

try this

 mapper.createmap<source, target>()         .formember(dest => dest.value,                     opt => opt.mapfrom                    (src => src.value1.startswith("a") ? src.value1 : src.value2)); 

condition option used add conditions properties must met before property mapped , mapfrom option used perform custom source/destination member mappings.


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? -