Sharepoint 编码标准

发布于 2024-12-01 08:28:57 字数 278 浏览 2 评论 0原文

我刚刚遇到这张表:

SPList.Items 的替代方案

请让我知道对于最后 5 项,差 --> 更好有什么区别。

I just came accross this table:

Alternatives to SPList.Items

Please let me know what difference in poor-->better for the last 5 items.

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

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

发布评论

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

评论(2

滿滿的愛 2024-12-08 08:28:57

这一切的原因很简单。当您编写 SPList.Items.Count 来获取项目总数时,SPList.Items 返回列表中所有项目的集合
您不需要所有物品,这可能是一个昂贵的行为。
通过编写 SPList.ItemCount,您可以确保只从数据库中读取数字,而不是所有项目。

本质上,这对于列表中的所有项目都是如此 - 您通常应该尽可能避免使用整个 Collection 对象(即 SPList.ItemsSPFolder.Files)。同样,如果多次使用它们,则应该使用局部变量缓存它们。

这是使用索引器的示例。假设我有一个 Guid,并且想要获取一个项目。

SPListItem item = list.Items[guid];

看起来很无辜,但实际上是一样的:

SPListItemCollection items = list.Items;
SPListItem item = items[guid];

要点是 - SharePoint(实际上是 C#)不知道您下一步要做什么,或者您将如何使用该集合。当您编写 .Items 时,您已经进行了缓慢的操作。

The reason for all of this is quite simple. When you write SPList.Items.Count to get the total number of items, SPList.Items returns the collection of all items in the list.
You don't want the all items, this can be an expensive action.
By writing SPList.ItemCount, you make sure you only read a number from the database, and not all items.

Essentially, this is true for all items in the list - you should generally avoid using the entire Collection objects (i.e. SPList.Items or SPFolder.Files) when you can. Similarly, if you use them more than once, you should cache them using a local variable.

Here's an example using indexers. Suppose I have a Guid, and want to get an item.

SPListItem item = list.Items[guid];

Looks innocent enough, but it is actually the same as:

SPListItemCollection items = list.Items;
SPListItem item = items[guid];

The point is - SharePoint (and C#, really) doesn't know what you're going to do next, or how you're going to use the collection. The moment you've wrote .Items you already made a slow operation.

诠释孤独 2024-12-08 08:28:57

这一切的原因很简单。当您编写 SPList.Items.Count 来获取项目总数时,SPList.Items 返回列表中所有项目的集合。
您不需要所有物品,这可能是一个昂贵的行为。
通过编写 SPList.ItemCount,您可以确保只从数据库中读取一个数字,而不是所有项目。

本质上,这对于列表中的所有项目都是如此 - 您通常应该尽可能避免使用整个 Collection 对象(即 SPList.Items 或 SPFolder.Files)。同样,如果多次使用它们,则应该使用局部变量缓存它们。

这是使用索引器的示例。假设我有一个 Guid,并且想要获取一个项目。

SPListItem item = list.Items[guid];

看起来很无辜,但实际上是一样的:

SPListItemCollection items = list.Items;
SPListItem item = items[guid];

关键是 - SharePoint(实际上是 C#)不知道您下一步要做什么,也不知道您将如何使用该集合。当您编写 .Items 时,您的操作就已经很慢了。

he reason for all of this is quite simple. When you write SPList.Items.Count to get the total number of items, SPList.Items returns the collection of all items in the list.
You don't want the all items, this can be an expensive action.
By writing SPList.ItemCount, you make sure you only read a number from the database, and not all items.

Essentially, this is true for all items in the list - you should generally avoid using the entire Collection objects (i.e. SPList.Items or SPFolder.Files) when you can. Similarly, if you use them more than once, you should cache them using a local variable.

Here's an example using indexers. Suppose I have a Guid, and want to get an item.

SPListItem item = list.Items[guid];

Looks innocent enough, but it is actually the same as:

SPListItemCollection items = list.Items;
SPListItem item = items[guid];

The point is - SharePoint (and C#, really) doesn't know what you're going to do next, or how you're going to use the collection. The moment you've wrote .Items you already made a slow operation.

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