从员工类创建组织结构

时间:2018-09-05 08:27:52

标签: c#

我有以下课程:

public class Employee
{
    public int EmployeeID { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeLastname { get; set; }
    public int ManagerID { get; set; }
    public Employee Manager { get; set; }
    public string Department { get; set; }
}

以及以下对象:

Employee emp1 = new Employee();
emp1.Department = "C Level";
emp1.EmployeeID = 1;
emp1.EmployeeLastname = "Smith";
emp1.EmployeeName = "Joe";

Employee emp2 = new Employee();
emp2.Department = "B Level";
emp2.EmployeeID = 2;
emp2.EmployeeLastname = "Smith";
emp2.EmployeeName = "John";
emp2.ManagerID = 1;

Employee emp3 = new Employee();
emp3.Department = "A Level";
emp3.EmployeeID = 3;
emp3.EmployeeLastname = "Mallari";
emp3.EmployeeName = "Lem";
emp3.ManagerID = 2;

我需要创建一个需要员工ID并返回一个包含org结构的Employee对象的方法。因此,例如,如果我搜索员工1,它将返回以下内容:

Employee
  EmployeeID: 1
  EmployeeName: Joe
  EmployeeLastName: Smith
  Department: C Level
  ManagerID: null
  Manager: null

但是例如,如果我搜索雇员ID 3,它将返回以下内容:

Employee
  EmployeeID: 3
  EmployeeName: Lem
  EmployeeName: Mallari
  Department: A Level
  ManagerID: 2
  Manager:
    EmployeeID: 2
    EmployeeName: John
    EmployeeLastname: Smith
    Department: B Level
    ManagerID: 1
    Manager: 
      EmployeeID: 1
      EmployeeName: Joe
      EmployeeLasname: Smith
      Department: C Level
      ManagerID: null
      Manager: null

我已经能够创建一个方法,该方法获取给定ID的经理的详细信息,并将该经理链接到最初搜索的员工。我只是想不出一种方法,可以持续搜索经理并更新对象的属性,从而能够创建所需的结构。

4 个答案:

答案 0 :(得分:0)

您的要求已经在C#中。

var employee3 = emps.FirstOrDefault(x => x.EmployeeID == 3);

您的employee3已包含管理者的所有信息。

var manager = employee3?.Manager?.Manager;

答案 1 :(得分:0)

一种解决方案是使用递归来重建对象图。缺点是您需要所有员工都在内存中。

public class EmployeeSearcher
{
    private IEnumerable<Employee> _employees;

    public EmployeeSearcher(IEnumerable<Employee> employees) => _employees = employees;

    public Employee Search(int id)
    {
        var employee = _employees.SingleOrDefault(e => e.EmployeeID == id);

        if (employee == null) throw new Exception($"Employee not found: {id}");
        FindManagerRecursive(employee);

        return employee;
    }
    private void FindManagerRecursive(Employee employee)
    {
        if (!employee.ManagerID.HasValue) return;
        var manager = _employees.SingleOrDefault(e => e.EmployeeID == employee.ManagerID.Value);

        if (manager == null) throw new Exception("Manager not found: {employee.ManagerID}");
        employee.Manager = manager;
        FindManagerRecursive(manager);
    }
}

用法:

var searcher = new EmployeeSearcher(new List<Employee> { emp1, emp2, emp3});
var result = searcher.Search(3);

答案 2 :(得分:0)

如果我是我,我将使用一个sql stroed过程来获取该区域。

示例:

创建过程[dbo]。[GetInformationByEmployeeID]

@EmployeeID int

AS

开始

您的Sql查询 (您的结果表将包含所有可能的列。也为空列。)

END

我将这个值称为Gridview等工具之一。

答案 3 :(得分:0)

在下面随时查看一下,您将知道谁都是雇员,谁是经理,然后借助您的雇员对象,您可以上下移动。

class Program
    {
       static List<Employee> lstEmployee = new List<Employee>();
        static void Main(string[] args)
        {

            Employee emp1 = new Employee();
            emp1.Department = "C Level";
            emp1.EmployeeID = 1;
            emp1.EmployeeLastname = "Smith";
            emp1.EmployeeName = "Joe";
            lstEmployee.Add(emp1);

            Employee emp2 = new Employee();
            emp2.Department = "B Level";
            emp2.EmployeeID = 2;
            emp2.EmployeeLastname = "Smith";
            emp2.EmployeeName = "John";
            emp2.ManagerID = 1;
            lstEmployee.Add(emp2);

            Employee emp3 = new Employee();
            emp3.Department = "A Level";
            emp3.EmployeeID = 3;
            emp3.EmployeeLastname = "Mallari";
            emp3.EmployeeName = "Lem";
            emp3.ManagerID = 2;
            lstEmployee.Add(emp3);

            Console.ReadLine();
        }

        public class Employee
        {
            public int EmployeeID { get; set; }
            public string EmployeeName { get; set; }
            public string EmployeeLastname { get; set; }
            public int ManagerID { get; set; }
            public Employee Manager
            {
                get
                {
                    return (from a in lstEmployee where a.EmployeeID == ManagerID select a).FirstOrDefault();
                }
            }
            public List<Employee> Reportees
            {
                get
                {
                    return (from a in lstEmployee where a.ManagerID == EmployeeID select a).ToList();
                }
            }
            public string Department { get; set; }
        }
    }