Home > Software engineering >  How to add details of two different list into another list in c#
How to add details of two different list into another list in c#

Time:12-09

Here i created the three classes and three lists.

I was able to successfully add project and employee to their respective lists.

Now i am creating a new Class and List to add employee to the project

public class Project
{
    public int id { get; set; }
    
    public Project(int Id)
    {
        this.id = Id;
    }
    public Project()
    {

    }


public class Employee
{
    public int EmployeeID { get; set; }


    public Employee(int empid)
    {
        this.EmployeeID = empid;
    }

    public Employee()
    {

    }

public class ProjectManagement
{
    public List<Project> Projects = new List<Project>();

    //Method for adding projects
    public void AddingProjects(Project project)
    {
        Projects.Add(project);
    }

public class EmpManagement
{
    public List<Employee> empList = new List<Employee>();

    //Method for adding new employee
    public void AddEmp(Employee emp)
    {
        empList.Add(emp);
    }


public class AddEmptoProject
{
    public string ProjectName { get; set; }
    public string EMPfirstName { get; set; }
    public AddEmptoProject(string projectName, string empfirstname)
    {
        this.ProjectName = projectName;
        this.EMPfirstName = empfirstname;
        }

    public AddEmptoProject()
    {
       
    }
}

I want to add and remove employees to the project

One project can have several number of employees

what is the logic ?

A code can help a bit more

CodePudding user response:

That depends on how you want your data structured. One way could be to add a property of type List<Employee> to the Project class. Then add a method to add an Employee to that list.

An example code could look something like:

public class Project {
  private List<Employee> AssociatedEmployees { get; } = new();
  
  public AddEmployee(Employee employee) {
    AssociatedEmployees.Add(employee);
  }
}

CodePudding user response:

To me, it sounds like you should add a property to your Project class to represent a list of Employees, all of which are working on the project. Your new Project class structure may look like the following:

public class Project
{
    public int id { get; set; }
    private List<Employee> employees { get; set; }

    public Project(int Id)
    {
        this.id = Id;
    }
    public Project()
    {

    }

    public AddEmployee (Employee e)
    {
        # Maybe add logic to prevent Exceptions from adding identical employee 
        # objects to the same list.
        employees.Add(e)
    }
}
  • Related