我有一个77个SPListItem对象的集合。这些对象可以具有对其他对象的隐式递归引用*。此层次结构中当前有4个级别。
我遇到的问题是,当我获得层次结构中更深层次的项目时,检索它们需要惊人的长时间。我在每个级别看到的时间:
zeroth: nearly instant
first: 2 seconds
second: 20 seconds
third: goes for about a minute and then times out以下是SPListItem对象中字段的结构:
ID
Title
ParentId //recursive field下面是我用来获取每个级别的SPListInformation的代码:
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();有没有人能看到我在这里做的可能导致我看到的长运行时间的事情?
如果有人想看数据,请让我知道。我省略了它,因为它会有点长。
*当我说“隐含递归引用”时,我的意思是每个SPListItem对象中都有一个成员可以包含一个ID,并且这个ID引用了列表中的另一个对象,但这种关系并不强制。
发布于 2011-08-26 02:01:34
那么,您将为second中的navList中的每一项执行first查询……每次执行first查询时,都会再次对navList中的每一项执行zero查询。third对每一项都会再做一次。
只需在每个查询的末尾添加一个对ToList的调用,可能会显着提高速度。
我们并不清楚您想要做什么,但我觉得您可以使用Dictionary或Lookup来更快地完成这项工作,而不是每次都要遍历整个集合。
发布于 2011-08-26 02:01:46
当您直接使用IEnumerable时,您将在每次遍历中重新枚举所有先前的枚举。我建议存储由ToList()创建的列表,并在后续调用中使用该列表。
https://stackoverflow.com/questions/7194995
复制相似问题