Question

Sorry about the inadequate information i provided originally

Here is my updated code sample and formatted.

EmployeeClass object

public class EmployeeClass
{
public int EId {get; set;}
public int EName {get; set;}
public List<Department> DeptList {get; set;}
public List<Area> AreaList {get; set;}
}

public class Department
{
    public int DepartmentID { get; set; }
    public string DepartmentName { get; set; }        
}
public class Area
{
    public int AreaID { get; set; }
    public string AreaName { get; set; }        
}

The requirement here is, I want to return the output class sortedEmployeeClass in a such a way that it will return List of sortedEmployeeClass object by expanding Department and Area

sortedEmployeeClass object extending EmployeeClass

public class sortedEmployeeClass : EmployeeClass
{
public string DepartmentName {get; set;}
public string  AreaName{get; set;}
}

The original object(EmployeeClass) has list of Employees with Department and Area as lists

But I want to return my destination object (sortedEmployeeClass) as List of Employees with Department Names , Area Names as strings

Hope this gives more visibility of what im looking for. Please let me know if you need more info.

For example If Department list has HR, Safety etc... & Area list has Facility1 and Facility2 etc...
My expected output would be....

EId Ename DepartmentName AreaName
1   Joe   HR             Facilty1
1   Joe   Safety         Facilty1
2   Jill  HR             Facilty2
2   Jill  Safety         Facilty2
Was it helpful?

Solution

Your value objects (VOs) are wrong, but using the same structure that you proposed the code is like this (remember, you need to create a list of employees):

//this is the list with combinations 
//---------------------------\/
List<sortedEmployeeClass> sortedList = new List<sortedEmployeeClass>();

foreach (var employee in employeeList)
{
   foreach(var department in employee.DeptList)
   {
      foreach(var area in employee.AreaList)
      {
          sortedEmployeeClass sorted = new sortedEmployeeClass();
          sorted.EId = employee.EId;
          sorted.EName = employee.EName;
          sorted.DepartmentName = department.Name;
          sorted.AreaName = area.AreaName;
          sortedList.Add(sorted);
      }
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top