I have matching data structures in a Source
and a destination
namespace for Employee
and Department
classes, with the following main difference:
Employee
has Employee
s nested property as List
.Employee
has nested property Employee
s 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>();
}