Automapper - Map a collection of strings to a collection property within a collection

Asked

Viewed 37 times

0

Which doubt ?

How to map Idcontributors (collection of strings) to a collection (Contributors), within the Taction collection, with a string property (Contributorid), using LINQ and autoMapper.

public ViewModelToDomainMappingProfile()
        {
            CreateMap<ActionViewModel, TAction>();
                //.ForMember(d => d.Contributors, opt => opt.MapFrom(a => ids = a.IdContributors.Select(x => { })));

        }

Models

Model Taction

public class TAction
    {
        public Guid Id {get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public ICollection<TActionContributor> Contributors { get; set; }
    }
public class TActionContributor
    {
        public Guid TActionId { get; set; }
        
        [ForeignKey("TActionId")] 
        public TAction Action { get; set; }
        
        public string ContributorId { get; set; }
        
        [ForeignKey("ContributorId")] 
        public ApplicationUser Contributor { get; set; }
    }

Actionviewmodel

public class ActionViewModel
    {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public ICollection<string> IdContributors { get; set; }
    }

1 answer

0

You need to create instances of Tactioncontributor.

CreateMap<ActionViewModel, TAction>();
            .ForMember(d => d.Contributors, opt => opt.MapFrom(a => ids = a.IdContributors
            .Select(x => new TActionContributor(){ ContributorId = x })));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.