mikeg22 wrote:
Its actually a bit more complicated than I first described.
We have a table called TableA. Table A has fields:
TableA.TableA_ID (PK)
TableA.TableB_ID (FK to an employee table)
TableA.LeftTableA_ID
TableA.RightTableA_ID
TableB represents a list of employees, and TableA defines their position in the company. The rule is for a given employee TableB_ID, there is a set of other TableB_IDs that are beneith this person in the company. This is what we are trying to find here. Here's an example row (that represents an employee) to explain the rule that defines the heirarchy in TableA.
TableA_ID = 1
TableB_ID = 1
TableA.LeftTableA_ID = 1
TableA.RightTableA_ID = 3
All employees under this employee would have LeftTableA_ID values between 1 and 3.
Ah weighted tree model ala Celko
(IIRC)
I guess you want for a given tableb_ID all employees beneath this person in the hierarchy?
I think the most efficient approach is a 2-fetch approach. This means: first fetch the TableA entity for that given tableb_ID, and then formulate a query like:
SELECT Employees.*
FROM Employees
WHERE Employees.ID IN
(
SELECT TableB_ID
FROM TableA
WHERE LeftTableA_ID BETWEEN leftValue AND rightValue
)
(I used 'leftValue and 'rightValue' as placeholders for variables, I don't know which db flavor you're using)
// first fetch TableA record for manager with employeeid located in _id
TableAEntity managerLocation = new TableAEntity();
managerLocation.TableB_ID = _id;
// I assume there is a unique constraint on TableB_ID, as an employee
// is located in the tree just once
adapter.FetchEntityUsingUniqueConstraint(managerLocation.ConstructFilterForTableB_ID());
// then formulate the query for our employees fetch
EntityCollection managedEmployees = new EntityCollection(new EmployeeEntityFactory());
RelationPredicateBucket filter = new RelationPredicateBucket();
IPredicateExpression subQueryFilter = new PredicateExpression();
subQueryFilter.Add(PredicateFactory.Between(
TableAFieldIndex.LeftTableA_ID, managerLocation.LeftTableA_ID, managerLocation.RightTableA_ID));
// now formulate the subquery. We specify the compare field Employee.ID and the setfield
// TableA.TableB_ID, and the filter we just setup:
// LeftTableA_ID BETWEEN leftValue AND rightValue, with the operator 'IN'.
filter.Add(new FieldCompareSetPredicate(
EntityFieldFactory.Create(EmployeeFieldIndex.ID), null,
EntityFieldFactory.Create(TableAFieldIndex.TableB_ID), null,
SetOperator.In, subQueryFilter));
// and fetch the employees
adapter.FetchEntityCollection(managedEmployees, filter);