Abe*_*ler 1 .net c# linq performance linq-to-objects
我有77个SPListItem对象的集合.这些对象可以对其他对象具有隐含的递归引用*.此层次结构中目前有4个级别.
我遇到的问题是,当我获得层次结构中较深的项目时,检索它们需要花费很长时间.我在每个级别看到的时间:
zeroth: nearly instant
first: 2 seconds
second: 20 seconds
third: goes for about a minute and then times out
Run Code Online (Sandbox Code Playgroud)
这是SPListItem对象中字段的结构:
ID
Title
ParentId //recursive field
Run Code Online (Sandbox Code Playgroud)
这是我用来获取每个级别的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();
Run Code Online (Sandbox Code Playgroud)
谁能看到我在这里做的事情可能导致我看到的长时间运行?
如果有人想看到这些数据,请告诉我.我把它留下了,因为它会有点冗长.
*当我说"隐含的递归引用"时,我的意思是每个SPListItem对象中都有一个成员可以包含一个ID,并且该ID引用列表中的另一个对象,但不强制执行此关系.
好吧,你在执行first查询的每个项目中navList的second...并在每次执行时first查询你在执行zero查询每个项目navList一次.third做这一切再次为每个项目.
只是ToList在每个查询结束时添加一个调用可能会显着加快速度.
你要做的事情并不是很清楚,但是每次你想要找到的东西时,感觉你可能会更快地使用一个Dictionary或者更快地Lookup迭代整个集合.
| 归档时间: |
|
| 查看次数: |
379 次 |
| 最近记录: |