Nested Multilevel List Mapping in Automapper in asp.net core 5 (.net5)

tech asuran 16 Reputation points
2021-08-17T09:08:26.877+00:00

HI All,

I am quite new to auto mapper

Requirement is to map the Source to destination. Need to create the destination model by mapping all the nested list in 3 levels Unfortunately we cannot change this nesting and structure , this is already mapped using custom mapper, we need to rewrite this using automapper.

Pasted both the source and destination at the bottom , in separate namespaces to identify

Can anybody help me to map the public List SkuDetails { get; set; }
in Source to public List SkuDetailsModel { get; set; } in Destination Below is my mapping profile, I can only able to map the direct Properies, list of list am not able to , I need to include all the mapping in one profile

Mapping Profile

public StockProfile()
        {
            CreateMap<Source, DestinationModel>()
                .ForPath(dest=>dest.StockInfoModel)

                CreateMap<Source, DestinationModel>().ForPath(dest => dest.DestiantionName, opt => opt.MapFrom(src => src.SourceName));

        }



  namespace SourceNameSpace 
  {
   public class Source
    {
        public string SourceName { get; set; }
        public List<StockDetails> StockDetails { get; set; }
    }


    public class StockDetails
    {
        public List<StockFeature> Features { get; set; }
    }


    public class StockFeature
    {
        public string Type { get; set; }
        public List<SkuInfo> SkuDetails { get; set; }
    }


    public class SkuInfo
    {
        public string Amount { get; set; }
        public string Type { get; set; }

    }
}


namespace DestinationNameSpace
{

    public class DestinationModel
    {

        public string DestiantionName { get; set; }

        public StockDetailModel StockDetailsModel { get; set; }
    }



    public class StockDetailModel : List<StockFeatureModel>
    {
        public string StockInfoModelName { get; set; }
    }


    public class StockFeatureModel
    {
        public string StockPriceAmt { get; set; }
        public string Type { get; set; }
        public int Additional { get; set; }

        public List<SkuInfoModel> SkuDetailsModel { get; set; }

    }

    public class SkuInfoModel
    {
        public string Amount { get; set; }
        public string AdditionalProperty1 { get; set; }
        public string AdditionalProperty2 { get; set; }

    }
}
Developer technologies ASP.NET ASP.NET Core
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.