Conditional mapping

Lucian Bargaoanu edited this page Dec 22, 2016 · 9 revisions

AutoMapper allows you to add conditions to properties that must be met before that property will be mapped.

This can be used in situations like the following where we are trying to map from an int to an unsigned int.

class Foo{
  int baz;

class Bar { 
  uint baz; 

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

Mapper.Initialize(cfg => {
    .ForMember(dest => dest.baz, opt => opt.Condition(src => (src.baz >= 0))); 

Similarly, there is a precondition. The difference is that it runs sooner in the mapping process, before the source value is resolved (think MapFrom or ResolveUsing). So the precondition is called, then we decide which will be the source of the mapping (resolving), then the condition is called and finally the destination value is assigned. You can see the steps yourself.