简体   繁体   English

反向SP列表

[英]reverse an SPlist

I need to extract the items from a list in reverse order (from last entry to the first). 我需要以相反的顺序(从最后一个条目到第一个)从列表中提取项目。 I've managed to get all the items but, from the first to the last. 我设法得到了所有物品,但是,从头到尾。 Here is part of the code I am using: 这是我正在使用的部分代码:

The List is on a different site collection. 该列表位于其他网站集上。

     using (SPSite oSite = new SPSite("urltolist"))
        {

            using (SPWeb oWeb = oSite.RootWeb)
            {
                SPList FItem = oWeb.Lists["List"];



                foreach (SPListItem item in FItem.Items)             
                {
                 //Display entries
                }
             }
       }

Get the SPListItemCollection , then iterate through it in reverse: 获取SPListItemCollection ,然后反向进行遍历:

var itemCollection = FItem.Items;
for(int i = itemCollection.Count - 1; i >= 0; i--)
{
    var item = itemCollection[i];
    //Display entries
}

If you just iterate over FItem.Items (ie call FItem.Items[i] repeatedly) you end up querying the list repeatedly, so don't do that. 如果仅遍历FItem.Items (即重复调用FItem.Items[i] ),则最终将反复查询列表,因此不要这样做。

You don't need to dispose of the RootWeb in this case. 在这种情况下,您不需要处理RootWeb。

using(var site = new SPSite("urltolist"))
{
    var list = site.RootWeb.Lists["List"];
    foreach(var li in list.Items.Cast<SPListItem>().Reverse().ToList())
    {
        // Display entries
    }
}

The question you need to ask is what order the items are to begin with - the way you do it, they'll be sorted by ID (analogous to time of creation). 您需要问的问题是,物品以什么顺序开始-您的操作方式将按照ID(类似于创建时间)进行排序。 If you want to sort them in another way, use an SPQuery with an OrderBy clause to retrieve the list items. 如果要以其他方式对它们进行排序,请使用带有OrderBy子句的SPQuery来检索列表项。

this can also be accomplished with a CAML Query. 这也可以通过CAML查询来完成。 Do you need to return ALL items? 您需要退回所有物品吗? If you can limit the query you can use the CAML node. 如果可以限制查询,则可以使用CAML节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM