title: IEnumerable not enumerating in foreach
body: <p>I'm encountering a problem with one of my <code>IEnumerable</code>'s that I haven't seen before.</p>
  
  <p>I have a collection:</p>
  
  <pre><code>IEnumerable&lt;IDependency&gt; dependencies;
  </code></pre>
  
  <p>that's being used in a <code>foreach</code> loop.</p>
  
  <pre><code>foreach (var dependency in dependencies)
  </code></pre>
  
  <p>For some reason this <code>foreach</code> doesn't iterate over my <code>IEnumerable</code> and simply skips to the end.</p>
  
  <p>If I change my <code>foreach</code> to loop through a a list however it seems to work fine:</p>
  
  <pre><code>foreach (var dependency in dependencies.ToList())
  </code></pre>
  
  <p>What could I be doing that's causing this behaviour? I haven't experienced this with <code>IEnumerable</code> before.</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>Here's the entire code of my <code>foreach</code> that's running in my method <code>GenerateDotString</code>:</p>
  
  <pre><code>foreach (var dependency in dependencies)
  {
      var dependentResource = dependency.Resource;
      var lineColor = (dependency.Type == DependencyTypeEnum.DependencyType.Hard) ? "blue" : "red";
  
      output += labelFormat.FormatWith(dependentResource.Name.MakeDotsafeString(), dependentResource.Name, dependentResource.ResourceType);
      output += relationshipFormat.FormatWith(dependentResource.Name.MakeDotsafeString(), currentName, lineColor);
  
      if (dependentResource.DependentResources != null)
      {
          output += GenerateDotString(dependentResource, dependentResource.DependentResources, searchDirection);
      }
  }
  
  return output;
  </code></pre>
  
  <p><strong>Update 2</strong>:</p>
  
  <p>Here's the signature of the method containing this foreach (incase it helps).</p>
  
  <pre><code>private static string GenerateDotString(IResource resource, IEnumerable&lt;IDependency&gt; dependencies, SearchEnums.SearchDirection searchDirection)
  </code></pre>
  
  <p><strong>Update 3</strong>:</p>
  
  <p>Here's the method <code>GetAllRelatedResourcesByParentGuidWithoutCacheCheck</code>:</p>
  
  <pre><code>private IEnumerable&lt;IDependency&gt; GetAllRelatedResourcesByParentGuidWithoutCacheCheck(Guid parentCiGuid, Func&lt;Guid, IEnumerable&lt;IDependency&gt;&gt; getResources)
  {
      if (!_itemsCheckedForRelations.Contains(parentCiGuid)) // Have we already got related resources for this CI?;
      {
          var relatedResources = getResources(parentCiGuid);
          _itemsCheckedForRelations.Add(parentCiGuid);
  
          if (relatedResources.Count() &gt; 0)
          {
              foreach (var relatedResource in relatedResources)
              {
                  relatedResource.Resource.DependentResources = GetAllRelatedResourcesByParentGuidWithoutCacheCheck(relatedResource.Resource.Id, getResources);
                  yield return relatedResource;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Update 4</strong>:</p>
  
  <p>I'm adding the methods in the chain here to be clear on how we're getting the collection of dependencies.</p>
  
  <p>The above method <code>GetAllRelatedResourcesByParentGuidWithoutCacheCheck</code> accepts a delegate which in this case is:</p>
  
  <pre><code>private IEnumerable&lt;IDependency&gt; GetAllSupportsResources(Guid resourceId)
  {
      var hardDependents = GetSupportsHardByParentGuid(resourceId);
      var softDependents = GetSupportsSoftByParentGuid(resourceId);
      var allresources = hardDependents.Union(softDependents);
      return allresources;
  }
  </code></pre>
  
  <p>which is calling:</p>
  
  <pre><code>private IEnumerable&lt;IDependency&gt; GetSupportsHardByParentGuid(Guid parentCiGuid)
  {
      XmlNode ciXmlNode = _reportManagementService.RunReportWithParameters(Res.SupportsHardReportGuid, Res.DependentCiReportCiParamName + "=" + parentCiGuid);
      return GetResourcesFromXmlNode(ciXmlNode, DependencyTypeEnum.DependencyType.Hard);
  }
  </code></pre>
  
  <p>and returns:</p>
  
  <pre><code>private IEnumerable&lt;IDependency&gt; GetResourcesFromXmlNode(XmlNode ciXmlNode, DependencyTypeEnum.DependencyType dependencyType)
  {
      var allResources = GetAllResources();
  
      foreach (var nodeItem in ciXmlNode.SelectNodes(Res.WebServiceXmlRootNode).Cast&lt;XmlNode&gt;())
      {
          Guid resourceGuid;
          var isValidGuid = Guid.TryParse(nodeItem.SelectSingleNode("ResourceGuid").InnerText, out resourceGuid);
  
          var copyOfResource = allResources.Where(m =&gt; m.Id == resourceGuid).SingleOrDefault();
          if (isValidGuid &amp;&amp; copyOfResource != null)
          {
              yield return new Dependency
              {
                  Resource = copyOfResource,
                  Type = dependencyType
              };
          }
      }
  }
  </code></pre>
  
  <p>which is where the concrete type is returned.</p>