question

techasuran-0190 avatar image
0 Votes"
techasuran-0190 asked ZhiLv-MSFT commented

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

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

dotnet-aspnet-core-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @techasuran-0190,

Whether you want to map the StockFeature class (include the SkuDetails model) to the StockFeatureModel (include the SkuInfoModel model), or mapping the Source model with its related entities to the DestinationModel?

If you want to map the Source model, please check the related properties in the Source model and DestinationModel, we can see that in the source model, the StockDetails is a list of StockFeature, but in the DestinationModel model, the StockDetailsModel is a StockDetailModel. So can you explain more detail about it. And it is better to add some comments at the end of the properties, so that we can know the property mapper conventions.

0 Votes 0 ·

0 Answers