c# - Hierarchy Employee/Manager from a List -


i have 3 classes.i want hierarchy of employees selected employee.

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 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 extension method hierarchy of employees selected person.first find direct employees of selected person ,  using extension method, find hierarchy                                list<int> hemployees = new list<int>(hierarchydownemployees.gethierarchyemployees(personkey));             (int = 0; < hemployees.count; i++)             {                 hemployees.addrange(hierarchydownemployees.gethierarchyemployees(hemployees[i]).except(hemployees));             }             //finding empname of hierarchy employees             hierarchydownemployeesname = empmgrdata.emplist().where(n => hemployees.any(s => s == n.empid)).tolist();              //displaying hierarchy employee names of selected person 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 employee hierarchy of selected person     public static ienumerable<int> gethierarchyemployees(this ienumerable<employeemanager> employees, int empid)     {         return emp in employees                emp.mgr_id == empid                select emp.emp_id ;     } } 

when select person in combobox, want show list of employees in hierarchy ('down'), based on employee/manager relationships, way bottom people have no employees reporting them.my code retrieves employees under selected person, not in hierarchical order.(when find employee name emplist)

  • example:
  • emp 20
  • >>emp 3
  • >>>>emp 1
  • >>emp 4
  • >>>>emp 5
  • >>>>>>>>emp 6
  • >>>>>>>>emp 7
  • >>>>>>>>emp 8
  • >>>>emp 2
  • >>emp 17
  • >>>>emp 19
  • >>>>emp 18

thanks help!

so think better approach modify extension method bit:

public static ienumerable<int> getemployees(this employeemanager mgr, int mgrid) {     return empmgrdata.empmgrlist()         .where(mgr => mgr.mgr_id = mgrid)         .select(mgr => mgr.emp_id); } 

and modify employeemanager class own employees:

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     {                 {             return this.getemployees(emp_id);         }     }      public employeemanager(int emid, int eid, int mid)     {         empmgr_id = emid;         emp_id = eid;         mgr_id= mid;     } } 

and finally, when person selected, go employeemanager. has of own employees, , employees have of own employees. code might this:

var rootmanager = empmgrdata.empmgrlist()     .firstordefault(mgr => mgr.emp_id = personkey); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -