0

I want to map an object of PersonDetails class to a list of Details objects. The PersonDetails class is

public class PersonDetails
{
    public PersonItems PersonItems {get;set;}
    public int CreatedBy {get;set;}
    public string PersonCode {get;set;}
}

public class PersonItems
{
    public string ItemCode {get; set;}
    public List<string> ItemColors {get;set;}
    public string ItemName {get; set;}
}

For each ItemColor is trying to map this to the following list of object classes:

public class Seller
{
    public string SellerCode {get;set;}
    public Details details {get; set;}
}

public class Details
{
    public string ItemCode {get; set;}
    public string ItemColor {get;set;}
    public string ItemName {get; set;}
    public string PersonCode {get;set;}
}

I have tried the following code by applying a loop on ItemColors in the service class, via AutoMapper.

List<Details> detailsList = new();
foreach(string ItemColor in PersonDetails.PersonItems.ItemColor)
{
    Details details = _mapper.Map<PersonDetails>( details) ;
    details.Add(details);
}

I have created a separate mapper class to register mapper methods.

public void RegisterPersonDetailstoDetailsObjectList()
{
     CreateMap<PersonDetails,Details>()
         .ForMember(d => d.details.PersonCode, src=> src.MapFrom(src => src.PersonCode
         .ForMember(d => d.PersonItems, src => src.MapFrom(s => new Details
             {
                 ItemCode = s.PersonItems.ItemCode,
                 **ItemColor = s.PersonItems.ItemColor,**
                 ItemName = s.PersonItems.ItemName
             }
         }));
}

Can't figure out to get each ItemColor from the loop in bold text and pass it to this AutoMapper method. Basically, I want to iterate over itemColor and assign the same to each list of objects mapped.

1

1 Answer 1

2

From my understanding of your question, you seem to map from the PersonDetails object to the List<Details> list.

You need the Custom Type Converter for the following mapping:

public void RegisterPersonDetailstoDetailsObjectList()
{
     CreateMap<PersonDetails, Details>()
        .ForMember(d => d.PersonCode, src=> src.MapFrom(src => src.PersonCode))
        .ForMember(d => d.ItemCode, src => src.MapFrom(src => src.PersonItems.ItemCode))
        .ForMember(d => d.ItemName, src => src.MapFrom(src => src.PersonItems.ItemName));

     CreateMap<PersonDetails, List<Details>>()
        .ConvertUsing((src, dest, ctx) =>
        {
            dest = new List<Details>();

            foreach (string itemColor in src.PersonItems.ItemColors)
            {
                var details = ctx.Mapper.Map<Details>(src);
                details.ItemColor = itemColor;

                dest.Add(details);
            }

            return dest;
        });
}

Demo @ .NET Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting error message at .ConvertUsing(src, dest, ctx) that Name src does not exist in the current context. Any thing else to be given in parameters?
.ConvertUsing((src, dest, ctx)) Wrap src, dest, ctx into (), these are the parameters for a Func.

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.