title: Error on System.Linq.Enumerable.<TakeIterator>d__3a`1.MoveNext()
body: <p>I have the following code to iterate over subwebs of a sharepoint site.
  If I debug it line by line, it never goes to  my catchs.</p>
  
  <p>however on the uls logs I have this exception which I dont know what it means or if I should worry</p>
  
  <pre><code>53b416d1-1497-4b40-beb5-cd261180ece8 Stack trace:   
   at Microsoft.SharePoint.SPWeb.get_Created()    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.&lt;&gt;c__DisplayClass8.&lt;LoadGridData&gt;b__7(SPWeb d)    
   at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)    
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)    
   at System.Linq.OrderedEnumerable`1.&lt;GetEnumerator&gt;d__0.MoveNext()    
   at System.Linq.Enumerable.&lt;TakeIterator&gt;d__3a`1.MoveNext()    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.&lt;&gt;c__DisplayClass8.&lt;LoadGridData&gt;b__5()    
   at Microsoft.SharePoint.SPSecurity.&lt;&gt;c__DisplayClass5.&lt;RunWithElevatedPrivileges&gt;b__3()    
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)    
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)    
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.LoadGridData()    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.OnPreRender(EventArgs e)    
   at System.Web.UI.Control.PreRenderRecursiveInternal()    
   at System.Web.UI.Control.PreRenderRecursiveInternal()    
   at System.Web.UI.Control.PreRenderRecursiveInternal()    
   at System.Web.UI.Control.PreRenderRecursiveInternal()    
   at System.Web.UI.Control.PreRenderRecursiveInternal()    
   at System.Web.UI.Control.PreRenderRecursiveInternal()    
   at System.Web.UI.Control.PreRenderRecursiveInternal()    
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)    
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)    
   at System.Web.UI.Page.ProcessRequest()    
   at System.Web.UI.Page.ProcessRequest(HttpContext context)    
   at Microsoft.SharePoint.Publishing.TemplateRedirectionPage.ProcessRequest(HttpContext context)    
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()    
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)    
   at System.Web.HttpApplication.PipelineStepManager.ResumeSteps(Exception error)    
   at System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb)    
   at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context)    
   at System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags)    
   at System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags)    
   at System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr pHandler, RequestNotificationStatus&amp; notificationStatus)    
   at System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr pHandler, RequestNotificationStatus&amp; notificationStatus)    
   at System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags)    
   at System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData, Int32 flags)
  </code></pre>
  
  <p>My code is as follows</p>
  
  <pre><code>private void LoadGridData()
          {
              try
              {
                  String currentUrl = SPContext.Current.Site.Url;
  
                  var jobInfoList = new List&lt;JobInfo&gt;();
  
                  SPSecurity.RunWithElevatedPrivileges(delegate
                  {
                      using (var clientSiteCollection = new SPSite(currentUrl))
                      {
                          foreach (
                              SPWeb web in
                                  clientSiteCollection.AllWebs.AsSafeEnumerable().Where(
                                      c =&gt;
                                      c.AllProperties[Constants.WebProperties.General.WebTemplate] != null &amp;&amp;
                                      c.AllProperties[Constants.WebProperties.General.WebTemplate].ToString() ==
                                      Constants.WebTemplates.JobWebPropertyName).OrderByDescending(d =&gt; d.Created).Take(5)
                              )
                          {
                              if (web.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName,
                                  SPBasePermissions.Open))
                              {
                                  SPList jobInfoListSp = web.Lists.TryGetList(Constants.Lists.JobInfoName);
                                  if (jobInfoListSp != null)
                                  {
                                      if (jobInfoListSp.Items.Count &gt; 0)
                                      {
                                          var value =
                                              new SPFieldUrlValue(
                                                  jobInfoListSp.Items[0][Constants.FieldNames.Job.iPowerLink].ToString());
  
                                          jobInfoList.Add(new JobInfo
                                          {
                                              JobName =
                                                  jobInfoListSp.Items[0][Constants.FieldNames.Job.JobName].ToString(),
                                              JobCode =
                                                  jobInfoListSp.Items[0][Constants.FieldNames.Job.JobCode].ToString(),
                                              IPowerLink = value.Url,
                                              JobWebsite = web.Url,
                                              IsConfidential =
                                                  HelperFunctions.ConvertToBoolean(
                                                      jobInfoListSp.Items[0][Constants.FieldNames.Job.Confidential]
                                                          .ToString())
                                          });
                                      }
                                  }
                              }
  
                              web.Dispose();
                          }
                      }
                  });
  
                  _lastCreatedJobsGrid.DataSource = jobInfoList;
                  _lastCreatedJobsGrid.DataBind();
              }
              catch (Exception ex)
              {
                  LoggingService.LogError(LoggingCategory.Job, ex);
              }
          }
  
  
  
   public static class SPWebCollectionExtensions
      {
          public static IEnumerable&lt;SPWeb&gt; AsSafeEnumerable(this SPWebCollection webs)
          {
              foreach (SPWeb web in webs)
              {
                  try
                  {
                      yield return web;
                  }
                  finally
                  {
                      web.Dispose();
                  }
              }
          }
      }
  </code></pre>
  
  <p>Any idea what could be the cause and solution if needed?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I made the query on a variable first, and then the foreach, I also removed the AsSafeEnumerable
  I wonder if I will have memoryleaks with this change?</p>
  
  <p><strong>Update 2: When trying to use the code in the first answer which is impressive, I still get disposing problems.</strong></p>
  
  <p>An </p>
  
  <pre><code>SPRequest object was not disposed before the end of this thread.  To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  This object will now be disposed.  Allocation Id: {96F3F1CB-DBD1-4514-AB9A-D49424AB0B6A}  This SPRequest was allocated
   at   
   at Microsoft.SharePoint.Library.SPRequest..ctor()    
   at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, SPAppPrincipalToken appPrincipalToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous)    
   at Microsoft.SharePoint.SPWeb.InitializeSPRequest()    
   at Microsoft.SharePoint.SPWeb.EnsureSPRequest()    
   at Microsoft.SharePoint.SPWeb.get_AllProperties()    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.&lt;&gt;c__DisplayClass8.&lt;LoadGridData&gt;b__6(SPWeb c)    
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()    
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)    
   at System.Linq.OrderedEnumerable`1.&lt;GetEnumerator&gt;d__0.MoveNext()    
   at xx.SP.DMS.WebParts.Extensions.Safe.&lt;SafeTake&gt;d__0`1.MoveNext()    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.&lt;&gt;c__DisplayClass8.&lt;LoadGridData&gt;b__5()    
   at Microsoft.SharePoint.SPSecurity.&lt;&gt;c__DisplayClass5.&lt;RunWithElevatedPrivileges&gt;b__3()    
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)    
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)    
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.LoadGridData()    
   at xx.SP.DMS.WebParts.WebParts.LastCreatedJobs.LastCreatedJobs.OnPreRender(EventArgs e)    
  </code></pre>