public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } public class PersonDto { public string FullName { get; set; } public int Age { get; set; } } var config = new MapperConfiguration(cfg => { cfg.CreateMap() .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}")); }); var person = new Person { FirstName = "John", LastName = "Doe", Age = 35 }; var mapper = config.CreateMapper(); var personDto = mapper.Map (person);
var config = new MapperConfiguration(cfg => { cfg.CreateMapIn this example, we've defined a list of Person objects and then used the MapperConfiguration object to map that list to a list of PersonDto objects. We've used the same mapping configuration as in the previous example. Overall, MapperConfiguration is a powerful tool for simplifying object mapping in C#. With it, you can easily configure mappings between properties of different objects and even map multiple objects at once.() .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}")); }); var people = new List { new Person { FirstName = "John", LastName = "Doe", Age = 35 }, new Person { FirstName = "Jane", LastName = "Smith", Age = 30 } }; var mapper = config.CreateMapper(); var peopleDto = mapper.Map >(people);