我具有以下示例结构:
public class Client
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Adress> AdressList { get; set; }
}
public class Adress
{
public Guid Id { get; set; }
public string street { get; set; }
}
我想将此结构自动映射到normalizr对javascript所做的事情。 我想要一个看起来像这个对象的结果
public class ViewModelRoot
{
public ICollection<ViewModelClient> ViewModelClientList { get; set; }
public ICollection<ViewModelAdress> ViewModelAdressList { get; set; }
}
public class ViewModelClient
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<string> AdressIdList { get; set; }
}
public class ViewModelAdress
{
public Guid Id { get; set; }
public string street { get; set; }
}
映射应将我的Client类中的AdressList提取到同一级别的单独列表中,并应仅将其References替换为其Guid。
我认为使用AutoMapper可以实现...任何想法? 提前谢谢。
答案 0 :(得分:0)
Mapper.Initialize(cfg=>
{
cfg.CreateMissingTypeMaps = false;
cfg.CreateMap<Client, ViewModelRoot>()
.ForMember(d=>d.ViewModelClientList, o=>o.MapFrom(s=>new[]{s}))
.ForMember(d=>d.ViewModelAdressList, o=>o.MapFrom(s=>s.AdressList));
cfg.CreateMap<Client, ViewModelClient>()
.ForMember(d=>d.AdressIdList, o=>o.MapFrom(s=>s.AdressList.Select(a=>a.Id)));
cfg.CreateMap<Adress, ViewModelAdress>();
});