I am trying to map the below class to a destination class using the below code. Here trying to map Employee class to
static void Main(string[] args)
{
var emp = new List<Employee>()
{
new Employee()
{
FirstName = "Test",
LastName = "Performance",
ID="1",
Availabities = new List<Availability>()
{
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(1)
}
}
},
new Employee()
{
FirstName = "Test1",
LastName = "Performance1",
ID="2",
Availabities = new List<Availability>()
{
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(10)
}
}
},
new Employee()
{
FirstName = "Test123",
LastName = "Performance1",
ID="3",
Availabities = new List<Availability>()
{
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(5)
},
new Availability()
{
BeginDate = DateTime.Now,
EndDate = DateTime.Now.AddDays(3)
}
}
}
};
Here is the destination object mapping, here the Employee to be mapped with EmployeeDest.
List<AvailabilityDest> empAvailabilitiesDest = new List<AvailabilityDest>();
var results = new List<EmployeeDest>();
foreach (var token in emp)
{
empAvailabilitiesDest.Clear();
foreach (var item in token.Availabities)
{
var empAvailability = new AvailabilityDest
{
BeginDateDest = item.BeginDate,
EndDateDest = item.EndDate,
};
empAvailabilitiesDest.Add(empAvailability);
}
var employee = new EmployeeDest
{
FirstNameDest = token.FirstName,
LastNameDest = token.LastName,
IDDest =token.ID,
AvailabitiesDest = empAvailabilitiesDest
};
results.Add(employee);
}
Console.WriteLine(results);
}
Here the empAvailabilitiesDest.Clear() is not clearing the list and the availabilityDest is getting increased with each iteration.
I am missing something here .
- How can I optimize the code here to get a better performance?