Sharepoint 编码标准
我刚刚遇到这张表:
请让我知道对于最后 5 项,差 --> 更好有什么区别。
I just came accross this table:
Please let me know what difference in poor-->better for the last 5 items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一切的原因很简单。当您编写
SPList.Items.Count
来获取项目总数时,SPList.Items
返回列表中所有项目的集合。您不需要所有物品,这可能是一个昂贵的行为。
通过编写
SPList.ItemCount
,您可以确保只从数据库中读取数字,而不是所有项目。本质上,这对于列表中的所有项目都是如此 - 您通常应该尽可能避免使用整个 Collection 对象(即
SPList.Items
或SPFolder.Files
)。同样,如果多次使用它们,则应该使用局部变量缓存它们。这是使用索引器的示例。假设我有一个 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
orSPFolder.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.
Looks innocent enough, but it is actually the same as:
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.这一切的原因很简单。当您编写 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.