aleczandru
aleczandru

Reputation: 5449

Mapping data from two objects two one with automapper

Hi I am using autommaper for data transfer from object and I have a case where I have to map from two objects to one.

This the data I get from the repositories:

IEnumerable<GetStudentClassmates_Result> students = UnitOfWork.Dashboard.GetStudentClassMates(studentId);
IEnumerable<GetStudentTeachers_Result> teachers = UnitOfWork.Dashboard.GetStudentTeachers(studentId);

This the object I need to map this to:

public class ParticipantsDTO
{
    public IEnumerable<GetStudentClassmates_Result> Students { get; set; }
    public IEnumerable<GetStudentClassmates_Result> Teachers { get; set; }
}

Is there any way to achieve this with autommaper?

Upvotes: 0

Views: 1226

Answers (2)

hutchonoid
hutchonoid

Reputation: 33306

Your example looks like they contain the same classes so it looks like they would not need mapping.

I would be expecting a GetStudentClassmates_ResultDto object.

Please correct me and I will update my answer.

This articles shows you exactly how to map collections:

https://github.com/AutoMapper/AutoMapper/wiki/Lists-and-arrays

First you need to create the map ie:

Mapper.CreateMap<GetStudentClassmates_Result, GetStudentClassmates_ResultDto>();

Then map them ie:

IEnumerable<GetStudentClassmates_ResultDto> ienumerableDest = Mapper.Map<IEnumerable<GetStudentClassmates_Result>, IEnumerable<GetStudentClassmates_ResultDto>>(students);

Upvotes: 1

Satpal
Satpal

Reputation: 133403

Probably You can wrap you IEnumerable<GetStudentClassmates_Result> and IEnumerable<GetStudentTeachers_Result> in a Tuple and define your Map based off that Tuple. The mapping code will look like this.

Mapper.CreateMap<Tuple<IEnumerable<GetStudentClassmates_Result>, IEnumerable<GetStudentTeachers_Result>>, ParticipantsDTO>();

Hope it works for you.

Upvotes: 2

Related Questions