Will the SPListItemCollection returned by the function reopen SPWeb?

After reading Stefan Gossner 's post about recycling objects and the question about the Cross-Deletion Method for Templates , I found that I was to blame for accidentally opening several SPWebs. I know that in a post by Stefan Gossner he mentions that you should get rid of SPWeb after you are done with some child object. However, the Microsoft documentation mentions the caching of the SPListItemCollection object. Is the following code correct? Will the returned SPListItemCollection reopen the SPWeb object? Is there any way to say for sure?

// is this correct????
private SPListItemCollection GetListItems()
{
    SPListItemCollection items = null;
    try
    {
        using (SPSite site = new SPSite(GetListSiteUrl()))
        {
            using (SPWeb web = site.OpenWeb())
            {
                // retrieve the list
                SPList list = web.Lists[_ListName];

                // more code to create the query...
                items = list.GetItems(query);
            }
        }
    }
    catch (Exception e)
    {
        // log error
    }
    return items;
}

Edit 09/09/09

:

SPWeb SPSite .

, , SPListItemCollection , SPWeb, ... SPWeb .

+5
3

, SPListItemCollection SPWeb . , , , INCORRECT, SPWeb SPListItemCollection.

: SPListItemCollection , .

private DataTable GetListItems()
{
    DataTable table = null;
    try
    {
        SPListItemCollection items = null;
        using (SPSite site = new SPSite(GetListSiteUrl()))
        {
            using (SPWeb web = site.OpenWeb())
            {
                // retrieve the list
                SPList list = web.Lists[_ListName];

                // more code to create the query...
                items = list.GetItems(query);

                // convert to a regular DataTable
                table = items.GetDataTable();
            }
        }
    }
    catch (Exception e)
    {
        // log error
    }
    return table;
}
+4

, , -

private void FetchItems(Action<SPListItemCollection> action)
{
   using(...)
   {
       var items = list.GetItems(query);
       action(items);
   }
}

, (), SPListItemCollection, :

FetchItems (items = > ....) FetchItems (DoStuffWithItems (SPListItemCollection))

+1

, SPWeb , SPListItemCollection, , .

, :

    private IEnumerable<SPListItem> AllItems;

    public void GetItems()
    {
        var results = SPContext.Current.Web.Lists[ListName].Items.Cast<SPListItem>();
        this.AllItems = results;
    }

and then I use AllItems everywhere and it works great.

If you are interested, then the action is carried out, so I can use Linq in the result set - much faster than sending a request to the list, especially if you make several subqueries in the data.

0
source

All Articles