2016-07-22 98 views
0

如果我有以下類:如何使用AutoMapper將一個列表映射到兩個列表?

public MainModel 
{ 
    public List<ChildModel> Children {get; set;} 
} 

public ChildModel 
{ 
    public bool IsDifferent {get; set;} 
} 

public MainDto 
{ 
    public List<ChildDto> Children {get; set;} 
    public List<DifferentChildDto> Different {get; set;} 
} 

public ChildDto 
{ } 

public DifferentChildDto 
{ } 

使用AutoMapper,是有可能分裂,映射ChildModel列表分爲2只獨立列出了基於財產?

最終結果應該是IsDifferent屬性集的項目將位於Different列表中,而其餘項目位於Children列表中。

映射也應該反向工作,即。將兩個DTO列表合併到1個模型列表中。

+0

關於反向映射:爲有序的收藏列表定義,所以你怎麼能指望項目的順序合併名單? – grek40

+0

@ grek40順序不重要。 –

回答

0
Mapper.CreateMap<MainModel, MainDto>() 
    .ForMember(d => d.Children, o => o.MapFrom(s => s.Children.Where(c => !c.IsDifferent))) 
    .ForMember(d => d.Different, o => o.MapFrom(s => s.Children.Where(c => c.IsDifferent))); 

Mapper.CreateMap<ChildModel, ChildDto>(); 

Mapper.CreateMap<ChildModel, DifferentChildDto>(); 

扭轉這一不爲小事,你可能會更好張貼,作爲一個單獨的問題

+0

['Enumerable.Concat'](https://msdn.microsoft.com/en-us/library/bb302894(v = vs.100).aspx)是不是微不足道的?編輯:好吧,也許不同的類型是一個問題 – grek40

+0

,因爲列表將不會有相同的類型(ChildrenDto與DifferentChildDto) –

相關問題