Reputation: 10335
I have objects :
class Group {
public string GroupName ;
public List<Access> Details ;
}
Class Access {
public int Id;
public string Name;
}
Now I have a Dto like:
Class GroupDto
{
string GroupName;
List<int> Details ;
}
User can create/send GroupDto and converted to Group before saving.
how to define the MappingProfile for this ?
Upvotes: 1
Views: 2884
Reputation: 1899
Like this:
Mapper.CreateMap<Group, GroupDto>()
.ForMember(d => d.Details,
opt =>
opt.MapFrom(
s => s.Details.Select(x=>x.Id).ToList()))
Upvotes: 1
Reputation: 23230
In your mapping profile just use MapFrom
method and let AutoMapper know how to get the data like below:
CreateMap<Group, GroupDto>()
.ForMember(
destination => destination.Details,
options => options.MapFrom(
source => source.Details.Select(detail => detail.Id).ToList()
)
);
Side Note: please expose a property instead of field like you did in your sample. Also make sure that properties on GroupDto
class are public
too.
Upvotes: 3