> >  First, simply fetch the employees in full:
> > var q = from e in ctx.Employees select e;
> 
> Excellent description, Frans... one thing I like to point out is that
> if you make your initial (flat) query from the database ordered by the
> parent id and entry id, you get "in-order insertion" behavior when
> building the tree, which makes everything faster:
> 
> SELECT ManagerID, EmployeeID, Name
> FROM Employee
> ORDER BY ISNULL(ManagerID, 0), EmployeeID

        No, that's not true. It's only true if employees at level n have all 
lower ID's than the employees at level n+1:

managerID | EmployeeID | Name
-----------------------------------
0         | 20         | John D. Rockefeller
1         | 4          | Rick W. Annabee
1         | 6          | Al MostThere

etc.

:)
So a sorting COULD give a better performance but the sorting should be taylored 
towards the ID distribution across the layers in the hierarchy. For example 
your sorting could have devastating results for performance in the situation 
where all managers have HIGHER ID values than their employees, as every row 
except the root will end up in the orphaned list ;)

                FB

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to