如何从多个列表中对 SPListItems 列表进行最佳排序?
我的问题更多的是“有更好的方法吗”问题,而不是“我该怎么做”问题。我的代码如下:
private List<SPListItem> GetListItemsFromSite(SPSite spSite)
{
List<SPListItem> listItems = new List<SPListItem>();
List<SPList> documentLibrariesInSite = GetCustomDocumentLibrariesInSite(spSite);
foreach (SPList spList in documentLibrariesInSite)
{
SPListItemCollection itemCollection = GetLatestDocumentsFromSPList(spList);
foreach(SPListItem item in itemCollection)
{
listItems.Add(item);
}
}
listItems.Sort((x, y) => ((DateTime) x[SPBuiltInFieldId.Created]).CompareTo(((DateTime) y[SPBuiltInFieldId.Created])));
listItems.Reverse();
return listItems;
}
private SPListItemCollection GetLatestDocumentsFromSPList(SPList spList)
{
SPQuery query = new SPQuery();
query.Query = "<Where><Gt><FieldRef Name=\"Created\" /><Value Type=\"DateTime\"><Today OffsetDays=\"-7\"</Value></Gt></Where>";
query.ViewAttributes = "Scope=\"Recursive\"";
query.RowLimit = NumberOfDocumentsToGet;
SPListItemCollection listItemCollection = spList.GetItems(query);
return listItemCollection;
}
private List<SPList> GetCustomDocumentLibrariesInSite(SPSite spSite)
{
List<SPList> listCollection = new List<SPList>();
foreach (SPWeb spWeb in spSite.AllWebs)
{
foreach (SPList spList in spWeb.Lists)
{
if (spList.TemplateFeatureId == CustomContentTypeFeatureGuid)
{
listCollection.Add(spList);
}
}
spWeb.Dispose();
}
return listCollection;
}
由于我希望在返回项目之前按创建日期对列表项进行排序,因此我根据 SPListItem 上的 CreatedDate 字段进行排序。我也不使用 SPSiteDataQuery 因为我想操作我得到的 SPListItems。
现在的问题是我是否有任何方法可以更有效或更“干净”地做到这一点?欢迎对此事有任何想法。
My question is more of a "Is there a better way to do it" question then a "How do I do it" question. My code is as follows:
private List<SPListItem> GetListItemsFromSite(SPSite spSite)
{
List<SPListItem> listItems = new List<SPListItem>();
List<SPList> documentLibrariesInSite = GetCustomDocumentLibrariesInSite(spSite);
foreach (SPList spList in documentLibrariesInSite)
{
SPListItemCollection itemCollection = GetLatestDocumentsFromSPList(spList);
foreach(SPListItem item in itemCollection)
{
listItems.Add(item);
}
}
listItems.Sort((x, y) => ((DateTime) x[SPBuiltInFieldId.Created]).CompareTo(((DateTime) y[SPBuiltInFieldId.Created])));
listItems.Reverse();
return listItems;
}
private SPListItemCollection GetLatestDocumentsFromSPList(SPList spList)
{
SPQuery query = new SPQuery();
query.Query = "<Where><Gt><FieldRef Name=\"Created\" /><Value Type=\"DateTime\"><Today OffsetDays=\"-7\"</Value></Gt></Where>";
query.ViewAttributes = "Scope=\"Recursive\"";
query.RowLimit = NumberOfDocumentsToGet;
SPListItemCollection listItemCollection = spList.GetItems(query);
return listItemCollection;
}
private List<SPList> GetCustomDocumentLibrariesInSite(SPSite spSite)
{
List<SPList> listCollection = new List<SPList>();
foreach (SPWeb spWeb in spSite.AllWebs)
{
foreach (SPList spList in spWeb.Lists)
{
if (spList.TemplateFeatureId == CustomContentTypeFeatureGuid)
{
listCollection.Add(spList);
}
}
spWeb.Dispose();
}
return listCollection;
}
Since I want the list items sorted by created date before i return the items i do a sort based on the CreatedDate field on the SPListItem. I also dont use the SPSiteDataQuery because i want to manipulate the SPListItems that i get.
Now the question is if i have any ways of doing this more efficient or "cleaner"? Any thoughts on the matter are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之后向查询添加 orderby 子句
您可以在 where 子句升序
降序
you could add an orderby clause to your query AFTER the where clause
Ascending
Descending
如果自定义列表中的项目具有公共托管属性或属性集,您可以为它们构建自定义搜索范围并查询此范围,而不是单独遍历每个列表。但是,如果您需要实时结果,这将不起作用,但如果您将增量爬网设置为足够频繁地运行,则可以更快、更轻松地获取项目,特别是如果您有许多自定义列表。
If the items in your custom lists have a common managed property or set of properties, you could build a custom search scope for them and query this scope instead of traversing each list individually. However, this will not work if you need real time results, but if you set up your incremental crawls to run frequently enough, you could get your items much faster and easier, especially if you have many of those custom lists.