从多个属性 Automapper 映射对象

Map objects from multiple properties Automapper

非常感谢您对完成给定场景映射的意见。

    class Section1
    {
        public string S1 { get; set; }
        public string S2 { get; set; }
        public string S3 { get; set; }
    }
    class Section2
    {
        public string S4 { get; set; }
        public string S5 { get; set; }
    }
    class Section3
    {
        public string S6 { get; set; }
        public string S7 { get; set; }
    }
    class SectionsInfo
    {
        public Section1 A { get; set; }
        public Section2 B { get; set; }
        public Section3 C { get; set; }
    }
    class SectionsTogetherInStraight
    {
        public string S1 { get; set; }
        public string S2 { get; set; }
        public string S3 { get; set; }
        public string S4 { get; set; }
        public string S5 { get; set; }
        public string S6 { get; set; }
        public string S7 { get; set; }           
    }

我有一个 class SectionsInfo 对象,想为 SectionsTogetherInStraight 配置 AutoMapper。提前致谢

您想使用Flattening. With your naming convention, you will need to use the IncludeMembers方法:

var configuration = new MapperConfiguration(cfg =>
{
    // Pull out the members of A, B, and C from SectionsInfo using IncludeMembers
    cfg.CreateMap<SectionsInfo, SectionsTogetherInStraight>()
        .IncludeMembers(s => s.A, s => s.B, s => s.C);

    // Map each section to the final destination (like normal).
    cfg.CreateMap<Section1, SectionsTogetherInStraight>();
    cfg.CreateMap<Section2, SectionsTogetherInStraight>();
    cfg.CreateMap<Section3, SectionsTogetherInStraight>();
});