AutoMapper如何展平嵌套列表

时间:2013-02-01 10:18:09

标签: automapper

我正在尝试使用automapper将一些嵌套列表映射到展平结构列表中。 目前,我能够使用姓名映射正确数量的信用,但我无法获得要映射的角色和角色描述。

我试图得到一个像这样的扁平化列表。

John Smith,演员,Evil博士

John Smith,作家,null

Frank Brown,导演,null

Joe Green,演员,Henchman

以下是我的域模型,视图模型和映射的缩减版本。 我对Domain.Credit到ViewModel.Credit映射做错了什么?

//Domain Model
public class Talent
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual IList<Credit> Credits { get; set; }
}

public class Credit
{
    public virtual Role Role { get; set; }
    public virtual string Subtitle { get; set; }
}

public class Role
{
    public virtual string Title { get; set; }        
}

//View Model
public class EpisodeViewModel
{
    public IList<Credit> Credits { get; set; }

    public class Credit
    {
        public string Name { get; set; }
        public string Role { get; set; }
        public string RoleDescription { get; set; }
    }
}

//Mappings
Mapper.CreateMap<Episode, EpisodeViewModel>()
    .ForMember(destination => destination.Credits, options => options.MapFrom(source => source.Talent));

Mapper.CreateMap<Talent, Credit>()
    .ForMember(destination => destination.Name, options => options.MapFrom(source => string.Format("{0} {1}", source.FirstName, source.LastName)));

Mapper.CreateMap<Domain.Credit, Credit>()
    .ForMember(destination => destination.Role, options => options.MapFrom(source => source.Role.Title))
    .ForMember(destination => destination.RoleDescription, options => options.MapFrom(source => source.Subtitle));

0 个答案:

没有答案