static void InsertUsers(List <IdName> returnValue, DepartmentTree node) { foreach (var user in DepartmentUsers) { if (node.Node.Id == user.DepartmentId) { bool found = false; foreach (var idName in returnValue) { if (idName.Id == user.UserId) { found = true; } } if (!found) { returnValue.Add(new IdName { Id = user.UserId, Name = UserBLL.GetNameById(user.UserId) }); } } foreach (var subNode in node.Children) { InsertUsers(returnValue, subNode); } } }
static public List <IdName> GetDepartmentUsersIdNames(int deptid) { List <IdName> list = new List <IdName>(); DepartmentTree node = FindNode(deptid, Root); if (node != null) { InsertUsers(list, node); } return(list); }
public static void UpdateDepartments() { Departments = (from o in db.Departments.AsNoTracking() select o).ToList(); DepartmentUsers = (from o in db.DepartmentUsers.AsNoTracking() select o).ToList(); foreach (var department in Departments) { if (department.PId == 0) { Root = new DepartmentTree { Node = department }; AddChildren(Root); break; } } }
static DepartmentTree FindNode(int deptid, DepartmentTree parent) { if (parent.Node.Id == deptid) { return(parent); } foreach (var node in parent.Children) { var found = FindNode(deptid, node); if (found != null) { return(found); } } return(null); }
static void AddChildren(DepartmentTree node) { if (node.Children == null) { node.Children = new List <DepartmentTree>(); } foreach (var department in Departments) { if (department.PId == node.Node.Id) { var child = new DepartmentTree { Node = department }; node.Children.Add(child); AddChildren(child); } } }