Weenhallo
Weenhallo

Reputation: 364

Ignore property with automap IF something

first time using automap and I want to do something like

public class Destination{
 pulic int Id {get;Set;}
 public string Name{get;set;}
 public string Age{get;set;}
}

if(something)
 var x = Mapping.Mapper.Map<Destination>(object) (with all properties)
else
 var y = Mapping.Mapper.Map<Destination>(object) (without Age property)

It's a dummy example

Upvotes: 1

Views: 107

Answers (1)

Ran Turner
Ran Turner

Reputation: 17966

You can use the Ignore() feature to strict members you will never map but these members are will be skipped in configuration validation.

What you might find usefull to use is the Condition() feature so you can map the member when the condition is true like the example below:

var configuration = new MapperConfiguration(cfg => {
   cfg.CreateMap<Foo,Bar>()
     .ForMember(dest => dest.baz, opt => opt.Condition(src => (src.baz >= 0)));
 });

In the above mapping example the property baz will only be mapped if it is greater than or equal to 0 in the source object.

Upvotes: 1

Related Questions