0

I would appreciate your inputs on accomplishing the mapping for the given scenario.

    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; }           
    }

I have an object for class SectionsInfo and would like to configure AutoMapper for SectionsTogetherInStraight. Thanks in advance

2

1 Answer 1

3

You want to use Flattening. With your naming convention, you will need to use the IncludeMembers method:

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>();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.