AutoMapper Tutorial

Conditional mapping

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.

public class Foo
{
  int baz;
}

public class Bar 
{ 
  uint baz; 
}

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

Mapper.CreateMap<Foo,Bar>()
  .ForMember(dest => dest.baz, opt => opt.Condition(src => (src.baz >= 0)); 

Here is full example:

using System; using AutoMapper; public ...

 

Page 5 of 7