title: How to do this with a fast method?
body: <p>I'm building a webpart for SharePoint 2010 that itereates through the whole site collection of a SharePoint site, and for each file within a document library it will save a value for each user registered at that site. Just for the record, there are over a hundred of users.</p>
  
  <p>So far, I've tried the following code:</p>
  
  <pre><code>public static void CalculateValues(MyDataContext db, string siteUrl)
  {
          SPWebCollection webCollection = new SPSite(siteUrl).AllWebs;
  
          Guid docLibFeatId = new Guid("00bfea71-e717-4e80-aa17-d0c71b360101");
  
          List&lt;int&gt; user_ids = (from u in db.Users
                             select u.Id).ToList();
  
          foreach (SPWeb web in webCollection)
          {
              foreach (SPList list in docLibraryColl)
              {
                  if (list.TemplateFeatureId == docLibFeatId &amp;&amp; !list.Hidden)
                  {
                      SPDocumentLibrary docLib = (SPDocumentLibrary)list;
  
                      SPFolder root = docLib.RootFolder;
  
                      TrackingUtility.GetFolderNode(db, user_ids, web, root);
                  }
              }
  
              web.Dispose();
          }
  }
  
  public static List&lt;SPFolder&gt; GetFoldersInFolder(CogitoRecommendationsDataContext db, SPFolder folder)
  {
      List&lt;SPFolder&gt; result = new List&lt;SPFolder&gt;();
  
      SPFolderCollection subFolders = folder.SubFolders;
      foreach (SPFolder subFolder in subFolders)
      {
          result.Add(subFolder);
      }
  
      return result;
  }
  
  public static void GetFolderNode(CogitoRecommendationsDataContext db, List&lt;int&gt; user_ids, SPWeb web, SPFolder folder)
  {
      List&lt;SPFolder&gt; folders = GetFoldersInFolder(db, folder);
  
      for (int j = 0; j &lt;= folders.Count - 1; j++)
      {
          SPFolder folderNode = folders[j];
  
          foreach (var id in user_ids)
          {
              SPUser spUser = web.AllUsers.GetByID(id);
  
              SaveValue(db, spUser, web, folderNode.UniqueId, SPAuditItemType.Folder);
          }
          SPFolder subfolder = folder.SubFolders[j];
  
          GetFolderNode(db, user_ids, web, subfolder);
      }
  }
  </code></pre>
  
  <p>So, when I execute the <code>CalculateValues</code> method, it takes too long for it to finish, and sometimes it doesn't even finish, because it throws an <code>OutOfMemory exception</code> (maybe there's some undisposed object?).</p>
  
  <p>Does anyone know about a faster way for doing this? I really don't know what else to try.</p>