0

I have these classes

    public class NamedEntityViewModel
    {
        public string Name { get; set; }
    }

    public class NamedEntityListViewModel
    {
        public List<NamedEntityViewModel> List { get; set; }
    }
    public class Album : INamedEntity
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public virtual ICollection<Song> Songs { get; set; }
        [Required]
        public int AlbumNumber { get; set; }
    }

I have a List<Album> and I want to map that to NamedEntityListViewModel with NamedEntityViewModel.Name mapping to Album.Name. How do I set this up in Automapper?

Tyler
  • 11,272
  • 9
  • 65
  • 105
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

1 Answers1

1

Try something like that:

First create you mapper:

Mapper.CreateMap<Album, NamedEntityViewModel>();

For the mapping do this:

// yourAlbumList is a List<Album>
var albumListVm = new NamedEntityListViewModel();
albumListVm.List = Mapper.Map<IEnumerable<NamedEntityViewModel>>(yourAlbumList);

This should do the job

JuChom
  • 5,717
  • 5
  • 45
  • 78