Automapper: Ignore on condition of

Posted by Kieranmaine on Stack Overflow See other posts from Stack Overflow or by Kieranmaine
Published on 2010-03-15T23:28:49Z Indexed on 2010/03/15 23:29 UTC
Read the original article Hit count: 501

Filed under:

Is it possible to ignore mapping a member depending on the value of a source property?

For example if we have:

public class Car
{
 public int Id { get; set; }
 public string Code { get; set; }
}

public class CarViewModel
{
 public int Id { get; set; }
 public string Code { get; set; }
}

I'm looking for something like

Mapper.CreateMap<CarViewModel, Car>()
 .ForMember(dest => dest.Code, opt => opt.Ignore().If(source => source.Id == 0))

So far the only solution I have is too use two different view model and create different mappings for each one.

© Stack Overflow or respective owner

Related posts about automapper