需要帮助解决我在使用 Linq-to-Objects 查询时遇到的性能问题吗?

发布于 2024-12-01 13:44:28 字数 2024 浏览 0 评论 0原文

我有 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 引用列表中的另一个对象,但不强制执行这种关系。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈场末日恋爱 2024-12-08 13:44:28

好吧,您正在 second 中对 navList 中的每个 项执行 first 查询...并且每次您执行 first 查询 您将再次对 navList 中的每个项目执行 zero 查询。 third 对每个项目都再次执行此操作。

只需在每个查询的末尾添加对 ToList 的调用可能会显着加快速度。

目前尚不清楚您要做什么,但感觉您可以使用 DictionaryLookup 来更快地完成此操作,而不是每次迭代整个集合当你想找点东西的时候。

Well, you're executing the first query for each item in navList in second... and each time you execute the first query you're executing the zero query for each item in navList again. third does it all again for every item.

Just adding a call to ToList at the end of each of those queries would probably speed it up significantly.

It's not really clear what you're trying to do, but it feels like you could probably make this rather quicker using a Dictionary or Lookup rather than iterating over the whole collection each time you want to find something.

○愚か者の日 2024-12-08 13:44:28

当您直接使用 IEnumerable 时,您将在每次传递时重新枚举所有先前的枚举。我建议存储由 ToList() 创建的列表并在后续调用中使用它。

You're re-enumerating all prior enumerations on each pass, as you're using the IEnumerable directly. I would recommend storing the list created by ToList() and using that in your subsequent calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文