jolynice
jolynice

Reputation: 544

Automapper , map list of long to an object nested

I have four classes

public class Status 
{
    public long Key { get;  set; }
    public string DisplayString { get; set; }
}

public class Attachment
{
    public long Key { get;  set; }
    public string FileName { get;  set; }
    public long FileSize { get; set; }
    public string ExternalKey_SO { get;  set; }
}


public class TaskClick 
{
    public long Key { get;  set; }
    public string CallId { get;  set; }
    public Status Status { get;  set; }
    public List<Attachment> Attachments { get;  set; }
}

public class TaskClickDto 
{
    public long Key { get;  set; }
    public string CallId { get; set; }
    public string Status { get; set; }
    public List<long> AttachmentKeys { get; set; }
}

I don´t know how to map a list of TaskClickDto.AttachmentKeys to TaskClick.Attachments

AttachmentKeys is a list of all the Keys of Taskclick

My automapper configuration

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {

        CreateMap<TaskClick, TaskClickDto>()
             //TaskClick --> TaskClickDto  works ok
            .ForMember(dest => dest.CallId, opt => opt.MapFrom(src => src.CallId))
            .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status.DisplayString))
            .ForMember(dest => dest.AttachmentKeys, opt => opt.MapFrom(src => src.Attachments.Select(k => k.Key)))
            .ReverseMap()    
            //TaskClickDto --> TaskClick  works Ko
            .ForPath(dest => dest.Status.DisplayString, opt => opt.MapFrom(src => src.Status))
            .ForPath(dest => dest.Attachments, ????));
    }
}

So I need to know how to create a new list of Attachment , for each one, map the key and ignore the rest of the properties.

Best regards. jolynice

Upvotes: 0

Views: 60

Answers (1)

stuartd
stuartd

Reputation: 73253

This should do it:

.ForPath(dest => dest.Attachments, opt => 
     opt.MapFrom(src => src.AttachmentKeys.Select(k => 
           new Attachment { Key = k }).ToList()));

Upvotes: 2

Related Questions