列表中的层次结构员工/经理

时间:2014-06-02 18:04:14

标签: c# winforms linq

我有三个班级。我想获得所选员工的员工等级。

public class Employees
{
    public int empId{ get; set; }
    public string empName { get; set; }
    public Employees(int id, string name)
    {
        empId= id;
        empName = name;
    }
}
public class EmployeeManager
{
    public int empmgr_id { get; set; }
    public int emp_id { get; set; }
    public int mgr_id{ get; set; }
    public EmployeeManager(int emid, int eid, int mid)
    {
        empmgr_id = emid;
        emp_id = eid;
        mgr_id= mid;
    }
}

public static class EmpMgrData
{
    public static IEnumerable<Employees> EmpList()
    {
        var lstEmp = new List<Employees>();
        lstEmp .Add(new Employees(1,"Emp 1"));
        lstEmp .Add(new Employees(2,"Emp 2"));
        lstEmp .Add(new Employees(3,"Emp 3"));
        lstEmp .Add(new Employees(4,"Emp 4"));
        lstEmp .Add(new Employees(5,"Emp 5"));
        lstEmp .Add(new Employees(6,"Emp 6"));
        lstEmp .Add(new Employees(7,"Emp 7"));
        lstEmp .Add(new Employees(8,"Emp 8"));
        lstEmp .Add(new Employees(9,"Emp 9"));
        lstEmp .Add(new Employees(10,"Emp 10"));
        lstEmp .Add(new Employees(11,"Emp 11"));
        lstEmp .Add(new Employees(12,"Emp 12"));
        lstEmp .Add(new Employees(13,"Emp 13"));
        lstEmp .Add(new Employees(14,"Emp 14"));
        lstEmp .Add(new Employees(15,"Emp 15"));
        lstEmp .Add(new Employees(16,"Emp 16"));
        lstEmp .Add(new Employees(17,"Emp 17"));
        lstEmp .Add(new Employees(18,"Emp 18"));
        lstEmp .Add(new Employees(19,"Emp 19"));
        lstEmp .Add(new Employees(20,"Emp 20"));

        return lstEmp ;
    }

    public static IEnumerable<EmployeeManager> EmpMgrList()
    {
        var lstEmpMgr = new List<EmployeeManager>
                                   {
                                       new EmployeeManager(1,18,19),
                                       new EmployeeManager(2,17,20),
                                       new EmployeeManager(3,19,17),
                                       new EmployeeManager(4,14,15),
                                       new EmployeeManager(5,13,15),
                                       new EmployeeManager(6,12,13),
                                       new EmployeeManager(7,9,13),
                                       new EmployeeManager(8,10,13),
                                       new EmployeeManager(9,11,13),
                                       new EmployeeManager(10,6,5), 
                                       new EmployeeManager(11,7,5), 
                                       new EmployeeManager(12,8,5), 
                                       new EmployeeManager(13,5,4), 
                                       new EmployeeManager(14,4,20), 
                                       new EmployeeManager(15,2,4), 
                                       new EmployeeManager(16,1,3), 
                                       new EmployeeManager(17,3,20)
                                   };
        return lstEmpMgr ;
    }

}
private void Form1_Load(object sender, EventArgs e)
    {
        //Displaying list of persons in the combo box
        var empList = EmpMgrData.EmpList();
        comboBox1.DataSource = empList;
        comboBox1.DisplayMember = "empName";
        comboBox1.ValueMember = "empId";
    }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(comboBox1.DisplayMember))
        {
            listBox1.DataSource = null;
            listBox1.Items.Clear();
            var personKey = ((Employees)comboBox1.SelectedItem).empId;
            var personname = ((Employees)comboBox1.SelectedItem).empName;
            List<EmployeeManager> hierarchyDownEmployees = null;
            List<Employees> hierarchyDownEmployeesName = null;
            hierarchyDownEmployees = EmpMgrData.EmpMgrList().ToList();
            //Using an Extension method to get the hierarchy of employees for the selected person.First we find all the direct employees of selected person and  using the extension method, we will find the hierarchy                   
            List<int> hEmployees = new List<int>(hierarchyDownEmployees.GetHierarchyEmployees(personKey));
            for (int i = 0; i < hEmployees.Count; i++)
            {
                hEmployees.AddRange(hierarchyDownEmployees.GetHierarchyEmployees(hEmployees[i]).Except(hEmployees));
            }
            //Finding the EmpName of the hierarchy employees
            hierarchyDownEmployeesName = EmpMgrData.EmpList().Where(n => hEmployees.Any(s => s == n.empId)).ToList();

            //Displaying the Hierarchy Employee Names of selected person to the list box
            if (hierarchyDownEmployeesName != null && hierarchyDownEmployeesName.Count() > 0)
            {
                label2.Text = "All Employees under " + comboBox1.Text + " are: " + string.Join(", ", hEmployees);
                listBox1.DataSource = hierarchyDownEmployeesName;
                listBox1.DisplayMember = "empName";
                listBox1.ValueMember = "empId";
                listBox1.SelectedIndex = -1;
            }                
        }
    }
}
public static class Hierarchy
{
    //Extension method to get the Employee Hierarchy of the selected person
    public static IEnumerable<int> GetHierarchyEmployees(this IEnumerable<EmployeeManager> employees, int empID)
    {
        return from emp in employees
               where emp.mgr_id == empID
               select emp.emp_id ;
    }
}

当我在ComboBox中选择一个人时,我希望根据员工/经理关系显示层次结构中的员工列表('down'),一直到没有员工报告的底层人员他们。我的代码检索所选人员下的所有员工,但不是按层次结构顺序检索。(当我从EmpList中找到员工姓名时)

  • 示例:
  • Emp 20
  • &gt;&gt; Emp 3
  • &gt;&gt;&gt;&gt; Emp 1
  • &gt;&gt; Emp 4
  • &gt;&gt;&gt;&gt; Emp 5
  • &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Emp 6
  • &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Emp 7
  • &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Emp 8
  • &gt;&gt;&gt;&gt; Emp 2
  • &gt;&gt; Emp 17
  • &gt;&gt;&gt;&gt; Emp 19
  • &gt;&gt;&gt;&gt; Emp 18

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

所以我认为更好的方法是稍微修改扩展方法:

public static IEnumerable<int> GetEmployees(this EmployeeManager mgr, int mgrID)
{
    return EmpMgrData.EmpMgrList()
        .Where(mgr => mgr.mgr_id = mgrID)
        .Select(mgr => mgr.emp_id);
}

然后修改EmployeeManager类以获得自己的员工:

public class EmployeeManager
{
    public int empmgr_id { get; set; }
    public int emp_id { get; set; }
    public int mgr_id{ get; set; }

    public IEnumerable<int> Employees
    {
        get
        {
            return this.GetEmployees(emp_id);
        }
    }

    public EmployeeManager(int emid, int eid, int mid)
    {
        empmgr_id = emid;
        emp_id = eid;
        mgr_id= mid;
    }
}

然后最后,当一个人被选中时,你就得到那个EmployeeManager。它现在拥有自己的所有员工,员工拥有自己的所有员工。该代码可能如下所示:

var rootManager = EmpMgrData.EmpMgrList()
    .FirstOrDefault(mgr => mgr.emp_id = personKey);