I have a collection of 77 SPListItem objects. These objects can have an implied recursive reference* to other objects. There are currently 4 levels in this hierarchy.
The issue that I'm running into is that when I get items that are deeper in the hierarchy it takes surprisingly long to retrieve them. Time I am seeing at each level:
zeroth: nearly instant
first: 2 seconds
second: 20 seconds
third: goes for about a minute and then times out
This is the structure of the fields in the SPListItem objects:
ID
Title
ParentId //recursive field
And this is the code I am using to get the SPListInformation at each level:
SPList navList = SPContext.Current.Web.Lists["NavStructure"];
//Get items that have no parent
var zero = from n in navList.Items.Cast<SPListItem>()
where ((SPFieldLookupValueCollection)n["Parent"]).Count == 0
select new { ID = n.ID, Title = n.Title };
//Get first level items
var first = from n in navList.Items.Cast<SPListItem>()
from z in zero
where ((SPFieldLookupValueCollection)n["Parent"]).Select(t => t.LookupId).Contains(z.ID)
select new { ID = n.ID, Title = n.Title, ParentId = z.ID};
lv_First.DataSource = first.ToList();
lv_First.DataBind();
//Get second level items
var second = from n in navList.Items.Cast<SPListItem>()
from z in first
where ((SPFieldLookupValueCollection)n["Parent"]).Select(t => t.LookupId).Contains(z.ID)
select new { ID = n.ID, Title = n.Title, ParentId = z.ID};
lv_Second.DataSource = second.ToList();
lv_Second.DataBind();
//Get third level items
var third = from n in navList.Items.Cast<SPListItem>()
from z in second
where ((SPFieldLookupValueCollection)n["Parent"]).Select(t => t.LookupId).Contains(z.ID)
select new { ID = n.ID, Title = n.Title, ParentId = z.ID};
lv_Third.DataSource = third.ToList();
lv_Third.DataBind();
Can anyone see something that I am doing here that could cause the long run times that I am seeing?
If anyone would like to see the data just let me know. I left it out because it would be a bit lengthy.
*When I say "implied recursive reference", I mean there is a member in each SPListItem object that can contain an ID and that this ID references another object in the list, but this relationship is not enforced.