public List <UnitTreeItemModel> GetChildrenEmployeeList(List <UnitTreeItemModel> childEmployees, Guid?departamentId) { List <List <UnitTreeItemModel> > BranchList = new List <List <UnitTreeItemModel> >(); List <UnitTreeItemModel> unitBranch; UnitTreeItemModel parentUnit = new UnitTreeItemModel(); List <UnitTreeItemModel> result = new ListStack <UnitTreeItemModel>(); int index = 0; foreach (var employee in childEmployees) { unitBranch = new List <UnitTreeItemModel>(); parentUnit = employee; unitBranch.Add(parentUnit); while (parentUnit.ParentId != null || parentUnit.id == departamentId) { parentUnit = (from e in db.Units where (e.Id == parentUnit.ParentId) select new UnitTreeItemModel() { id = e.Id, Type = e.Type, DataId = e.Id, expanded = true, Name = e.DisplayName, hasChildren = true, ParentId = e.ParentId, }).First(); unitBranch.Add(parentUnit); } BranchList.Add(unitBranch); } foreach (var item in BranchList) { foreach (var unit in item) { if (unit.id == departamentId) { index = item.IndexOf(unit); if (item[index - 1].Type == 1) { result.Add(item.First()); } } } } return(result); }
//return full treeview by search public List <UnitTreeItemModel> BuildTree(List <UnitTreeItemModel> employees) { List <UnitTreeItemModel> tree = new List <UnitTreeItemModel>(); List <List <UnitTreeItemModel> > BranchList = new List <List <UnitTreeItemModel> >(); List <UnitTreeItemModel> unitBranch; UnitTreeItemModel parentUnit = new UnitTreeItemModel(); foreach (var employee in employees) { unitBranch = new List <UnitTreeItemModel>(); parentUnit = employee; unitBranch.Add(parentUnit); while (parentUnit.ParentId != null) { parentUnit = (from e in db.Units where (e.Id == parentUnit.ParentId) select new UnitTreeItemModel() { id = e.Id, Type = e.Type, DataId = e.Id, expanded = true, Name = e.DisplayName, hasChildren = true, ParentId = e.ParentId }).First(); unitBranch.Add(parentUnit); } unitBranch.Reverse(); BranchList.Add(unitBranch); } foreach (var item in BranchList) { BuildChild(tree, item); } return(tree); }