2016-09-29 4 views
0

オブジェクトをマップしようとしていますが、実際にはうまく機能しません。ソースオブジェクトから宛先オブジェクトおよびそのサブオブジェクトのコレクションへのマップ

私は構造を持つソースオブジェクトを持っている:

Object Prod 
{ 
    object<type1> Attribute (this object has fields field A,field B etc); 
    List<type2> Species; 
} 

私の宛先オブジェクト:

Object C 
{ 
    field A, 
    field B 
    List<type3> subs 
} 

タイプ2とタイプ3の間TYPE1とオブジェクトCとマッピング間のマッピングがあります。しかし、type3サブリストは、オブジェクトprod種とsubs(コレクション)の間のマップが必要なので、常に空です。 ソースから宛先のサブオブジェクトコレクションへの値のマッピング方法。

答えて

0
public class Prod 
{ 
    public Type1 Attribute; 
    public List<Type2> Species; 
} 

public class C 
{ 
    public int A; 
    public string B; 
    public List<Type3> Subs; 
} 

public class Type1 
{ 
    public int A; 
    public string B; 
} 

public class Type2 
{ 
    public int C; 
} 

public class Type3 
{ 
    public int D { get; set; } 
} 

Mapper.CreateMap<Prod, C>() 
    .ForMember(d => d.A, o => o.MapFrom(s => s.Attribute.A)) 
    .ForMember(d => d.B, o => o.MapFrom(s => s.Attribute.B)) 
    .ForMember(d => d.Subs, o => o.MapFrom(s => s.Species)); 

Mapper.CreateMap<Type2, Type3>().ForMember(d => d.D, o => o.MapFrom(s => s.C)); 

var prod = new Prod 
{ 
    Attribute = new Type1 { A = 1, B = "1" }, 
    Species = new List<Type2> { new Type2 { C = 2 }, new Type2 { C = 3 } } 
}; 

var c = Mapper.Map<C>(prod); 
関連する問題