0

是否可以将 Group 中的 IList 映射到 GroupViewModel 中的 IEnumerable?

模型

public class Group
{
    public string Name { get; set; }
    public virtual IList<Permission> Permissions
    {
        get { return _permissoes ?? (_permissions = new List<Permission>()); }
        set { Permissoes.Clear(); _permissions = value; }
    }
}

public class Permission
{
    public long Id { get; set; }
    public string Name { get; set; }
}

视图模型

public class GroupViewModel
{
    public string Name { get; set; }
    public IEnumerable<PermissionViewModel> Permissions { get; set; }
}

public class PermissionViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}
4

1 回答 1

3

就那么简单:

var grp = new Group {Permissions = new List<Permission>{new Permission{Name="Permission 1"}, 
                                                       {new Permission{Name="Permission 2"}}}};

Mapper.CreateMap<Permission, PermissionViewModel>();

Mapper.CreateMap<Group, GroupViewModel>();


var result = Mapper.Map<GroupViewModel>(grp);
于 2013-01-16T20:27:42.667 回答