0

我有这些课程

    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; }
    }

我有一个List<Album>,我想NamedEntityListViewModel通过NamedEntityViewModel.Name映射到 Album.Name 来映射它。如何在 Automapper 中进行设置?

4

1 回答 1

1

尝试这样的事情:

首先创建你的映射器:

Mapper.CreateMap<Album, NamedEntityViewModel>();

对于映射,请执行以下操作:

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

这应该做的工作

于 2014-02-09T23:12:52.917 回答