static void Main(string[] args) { Mapper.Initialize(cfg => { cfg.CreateMap <Foo, FooDto>().ForMember(dest => dest.Age, opt => opt.Condition(src => (src.Age >= 0)));; }); var foo = new Foo() { Name = "Name", Address = "Address", Age = -1 }; var fooDTO = new FooDto(); Mapper.Map(foo, fooDTO); Console.ReadLine(); }
static void Main(string[] args) { Mapper.Initialize(cfg => { //Using specific type converter for specific arrays cfg.CreateMap <Foo[], FooDto[]>().ConvertUsing(new ArrayFilterTypeConverter <Foo[], FooDto[], Foo, FooDto>( (src, dest) => (src.Age > 0) )); cfg.CreateMap <Foo, FooDto>() .ForMember(dest => dest.Age, opt => opt.Condition(src => (src.Age >= 0))) .ForMember(dest => dest.CurrentAddress, opt => { opt.Condition(src => (src.Age >= 0)); opt.MapFrom(src => src.Address); }); }); var foo = new Foo() { Name = "Name", Address = "Address", Age = -1 }; var fooDTO = new FooDto(); var fooArray = new Foo[] { new Foo() { Name = "Name1", Address = "Address1", Age = -1 }, new Foo() { Name = "Name2", Address = "Address2", Age = 1 }, new Foo() { Name = "Name3", Address = "Address3", Age = 1 } }; var fooDTOArray = Mapper.Map <Foo[], FooDto[]>(fooArray); //get 2 elements instead of 3 Mapper.Map(foo, fooDTO); //The result is we skipped Address and Age properties becase Age is negative Console.ReadLine(); }
static void Main(string[] args) { Mapper.Initialize(cfg => { cfg.CreateMap <Foo, FooDto>() .ForMember(dest => dest.Age, opt => opt.Condition(src => (src.Age >= 0))) .ForMember(dest => dest.Address, opt => opt.Condition(src => (src.Age >= 0))); }); var foo = new Foo() { Name = "Name", Address = "Address", Age = -1 }; var fooDTO = new FooDto(); Mapper.Map(foo, fooDTO); //The result is we skipped Address and Age properties becase Age is negative Console.ReadLine(); }