I am trying to display between two lists of objects. The source type has a complex property of type A ; destination type is a flattened subset of type A plus an additional scalar property that is in the source type.
public class A { public int Id { get; set; } public string Name { get; set; } } public class Source { public A MyA { get; set; } public int SomeOtherValue { get; set; } } public class Destination { public string Name { get; set; } public int SomeOtherValue { get; set; } }
If this is not clear, I would like Source.MyA.Name display on Destination.Name and Source.SomeOtherValue to display on Destination.SomeOtherValue .
In fact, type A has a dozen or so properties, about which 80% go to properties with the same name in Destination . I can get the job to work if I explicitly describe the mappings in CreateMap as follows:
CreateMap<Source, Destination>() .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.MyA.Name));
The disadvantage here is that I do not want to add a ForMember line for each of the properties of A that need to be copied to Destination . I was hoping I could do something like:
CreateMap<Source, Destination>() .ForMember(dest => dest, opt => opt.MapFrom(src => src.MyA));
But if I try above, I get a runtime error when registering the mapping: "User configuration for members is supported only for individual top-level members for a type."
thanks
source share