Brandon
Brandon

Reputation: 3266

AutoMapper Map DTO Properties to Model List

I have a DTO object like this:

public class Original {

     public string Item1 { get; set; }
     public string Item2 { get; set; }
     public string Item3 { get; set; }
}

Then, I want to use AutoMapper to map those 3 items to a List<string> property on my model.

public class Model {

     public List<string> Items { get; set; }
}

I'm not even sure what to post as what I've already tried as it's just been nothing even close.

Upvotes: 0

Views: 238

Answers (1)

Vidmantas Blazevicius
Vidmantas Blazevicius

Reputation: 4802

You should be able to create a simple map for that:

CreateMap<Original, Model>()
     .ForMember(dest => dest.Items, opt => opt.MapFrom(src => new List<string> {dest.Item1, dest.Item2, dest.Item3});

Upvotes: 1

Related Questions