I have two types:
Source:
public class User
{
public string City { get; set; } = "";
public Guid Identifier { get; set; } = Guid.NewGuid();
}
Destination:
public class UserDTO
{
public Guid Id { get; set; } = Guid.NewGuid();
public List<Address> Address { get; set; }
}
public class Address
{
public string City { get; set; } = "";
}
How can I create a mapping with AutoMapper from the single property City to the Address list with the City property in it? This is my mapping now:
public UserMappings()
{
CreateMap<User, UserDTO>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Identifier));
}
You can create objects including arrays and list on the fly while mapping like below :
public UserMappings()
{
CreateMap<User, UserDTO>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Identifier))
.ForMember(dest => dest.Address,
opt => opt.MapFrom(
src => new List<Address> {
new Address { City = src.City }
}));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.