2012-06-11 59 views
1

有誰知道在每個映射條件下條件映射源屬性的方法(或解決方法)嗎?Automapper條件集合映射

此處的目的是基於Web服務操作參數條件映射子對象的集合。 如:

Parent GetParent(bool includeChildren); 

到目前爲止,唯一可行的解​​決方案,我發現是可以創建一個包裝類添加一個布爾屬性,如:

public class ParentMapper 
{ 
    Parent Parent; 
    public bool IncludeChildren {get;set;} 
} 

或者直接在添加IncludeChildren財產模特來源類,女巫我真的不喜歡,因爲混合的目的。

一個完美的解決辦法是這樣的:

TDestination Map<TSource, TDestination>(TSource source, bool includeCollections); 

但我不認爲我會得到任何運氣對於一個有效的解決方案。

任何幫助,將不勝感激...

回答

0

目前沒有什麼內置允許您實現這一點。你可以做以下,但:

var destinations = Mapper.Map<List<Parent>, List<ParentDto>>(
    sources.Where(source => source.Child !=null) 
); 

這樣做的另一種方法是:

config.CreateMap<Parent, ParentDto>() 
      .AfterMap((source, dest) => 
      { 
       if (source.Child !=null) 
       { 
        //do some stuff here 
       } 
      }); 
+0

@gjsduarte,你想我的答案嗎? –