AutoMapper issue to map nested array in IQueryable map

37 views
Skip to first unread message

manjay...@gmail.com

unread,
Apr 28, 2015, 12:42:16 AM4/28/15
to automapp...@googlegroups.com

I have matching data structures in a Source and a destination namespace for Employee and Department classes, with the following main difference:

  • Source Employee has Employees nested property as List.
  • Destination Employee has nested property Employees as Array.

I want to map these two classes but Employee property is causing a StackOverflowException.


namespace Soure
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Department> dept1 { get; set; }
        public List<Employee> employees { get; set; }
        public int age { get; set; }
        public Employee()
        {
            this.Id = 1;
            this.Name = "Test Employee  Name";
            this.age = 10;
        }
    }

    public class Department
    {
        public int Id { get; set; }
        public string DeptName { get; set; }
        public Department()
        {
            Id = 2;
            DeptName = "Test Dept";
        }
    }
}

namespace destination
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int age { get; set; }
        public Departments[] dept1 { get; set; }
        public Employee[] employees { get; set; }

    }

    public class Departments
    {
        public int Id { get; set; }
        public string DeptName { get; set; }
    }
}

This shows how I am mapping using AutoMapper
 static void Main(string[] args)
{
    var emp1 = new Soure.Employee();
    var emp2 = new Soure.Employee();
    var dept = new Soure.Department();
    var depts = new List<Soure.Department>();
    depts.Add(dept);
    emp1.dept1 = depts;
    emp2.dept1 = depts;

    var empList = new List<Soure.Employee>() { emp1, emp2 }.AsQueryable();
    Mapper.CreateMap<Soure.Employee, destination.Employee>();
    Mapper.CreateMap<Soure.Department, destination.Departments>();

    Mapper.AssertConfigurationIsValid();

    var xu = Mapper.Map<IEnumerable<Soure.Employee>, IEnumerable<destination.Employee>>(empList);
    var mappedEmp = empList.Project().To<destination.Employee>();
}
Reply all
Reply to author
Forward
0 new messages