title: Reference to IEnumerable of XElements
body: <p>I want to mutate a IEnumerable collection of XElement (change attributes, children etc) outside of the iteration loop it a separate method. I've discovered this isn't possible with references or assignment because the way IEnumerable just returns/yields a value.</p>
  
  <p>This method adds child nodes to an XElement:</p>
  
  <pre><code>protected static void setErrorOnItem(ref XElement item, DiagErrorCode errorCode, DiagStatus errorLevel) {
      XElement errorNode = new XElement("Error");
          errorNode.SetAttributeValue("errorCode", errorCode.ToString());
          errorNode.SetAttributeValue("errorLevel", errorLevel.ToString());
          item.Element("Errors").Add(errorNode);
      }
  </code></pre>
  
  <p>This works fine <em>unless</em> it's in the context of a loop of an XElement collection, which is handled by an IEnumerable iterator.</p>
  
  <p>It's fine to to set attributes on the node from within the loop. But how can I put this logic in it's own method if references and assignment cannot be done?</p>
  
  <pre><code>public IEnumerable&lt;XElement&gt; GetRevisedVariationOverTime(IEnumerable&lt;XElement&gt; items) {
    foreach(XElement item in items) {
      if(sampleSize &lt; RequiredSampleSize) {
        // Works fine...
        item.Attribute("Status").Value = DiagStatus.WARNING.ToString();
        // Can't assign
        item = getItemReplacement(item);
        // Can't reference
        setErrorOnItem(ref item);
      }
    }
  
    ...
    return items;
  }
  </code></pre>