1

I have 2 objects which need to be converted to Dto object using automapper. Those 2 objects are

    public class Parent
    {
        public int ParentID { get; set;}
        public string ParentCode { get; set; }
    }

    public class Child
    {
        public int ChildID { get; set; }
        public int ParentID { get; set;}
        public string ChildCode { get; set; }
    }

And 2 Dto objects

    public class ParentDto
    {
        public int ParentID { get; set; }
        public string ParentCode { get; set; }
        public List<ChildDTO> ListChild { get; set; }
    }

    public class ChildDto
    {
        public int ChildID { get; set; }
        public string ChildCode { get; set; }
    }

Need to convert 2 set of list

List<Parent> ListParent
List<Child> ListChild

To

List<ParentDto> ListParentDto

1 Answer 1

1

Because you have multiple sources, my solution was a method like

    public static List<ParentDto> MapToDto(List<Parent> parents, List<Child> childs)
    {
        List<ParentDto> parentDtos = new List<ParentDto>();
        var config = new MapperConfiguration(cfg => {

            cfg.CreateMap<Parent, ParentDto>().BeforeMap((s, d) =>
            {
                d.ListChild = new List<ChildDto>();
                foreach (var child in childs.Where(c => c.ParentID == s.ParentID))
                {
                    d.ListChild.Add(new ChildDto { ChildCode = child.ChildCode, ChildID = child.ChildID });
                }
            }).ForMember(dest => dest.ParentID, opt => opt.MapFrom(src => src.ParentID)).ForMember(dest => dest.ParentCode, opt => opt.MapFrom(src => src.ParentCode));
        });

        IMapper mapper = config.CreateMapper();

        foreach (var p in parents)
        {
            var source = p;
            var destination = mapper.Map<Parent, ParentDto>(source);
            parentDtos.Add(destination);
        }

        return parentDtos;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Don't understand the following code -> foreach (var child in childs.Where(c=>c.ParentID==s.ParentID)) -> what is childs ???
It works.. But i have lots of parent child Dto's ... can you help me with a generic method? thanks in advance bro.
Great :) Help me marking my answer as useful pls. Do you have more class's with this format is what you are saying? and that you want a generic method that will receive 2 lists like the one i posted and returns a result?
One solution is, to do 2 interfaces, IParent and IChild, and in the method the input params will be List<IParent> and List<IChild> with this you can have different class's but because they all implement the same interface that have the base params the method becomes generic.

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.