title: Why is ReSharper telling me that the local variable is unused and that I'm accessing a modified closure?
body: <p>I have the following code:</p>
  
  <pre><code>dataContext.Objects.DeleteAllOnSubmit(dataContext.Objects.Where(o =&gt; o.Id == object.Id));
  </code></pre>
  
  <p>ReSharper is underlining <code>object.Id</code> and telling me:</p>
  
  <blockquote>
    <p>Access to modified closure</p>
  </blockquote>
  
  <p>I understand why it's doing this and have changed the code to:</p>
  
  <pre><code>int localId = object.Id; 
  dataContext.Objects.DeleteAllOnSubmit(dataContext.Objects.Where(o =&gt; o.Id == localId));
  </code></pre>
  
  <p>But now ReSharper is giving me <strong>two</strong> underlines. The first is under <code>localId</code> where it's declared and assigned:</p>
  
  <blockquote>
    <p>Value assigned is not used in any execution path</p>
  </blockquote>
  
  <p>and the second is under <code>localId</code> where it's used in the query and is still:</p>
  
  <blockquote>
    <p>Access to modified closure</p>
  </blockquote>
  
  <p>Why is this happening? Is the local variable being optimised out so that I'm still effectively accessing the object's field in the query?</p>
  
  <p>Is this something I need to worry about? The code appears to be working correctly, but I'm trying to ensure that we've not left any time-bombs lying around.</p>
  
  <p>Is there anything I can do (other than just ignoring these messages) to solve the problem?</p>
  
  <p>Splitting the code like this and realising the query:</p>
  
  <pre><code>int localId = object.Id;
  var objectsToDelete = dataContext.Objects.Where(o =&gt; o.Id == localId)).ToList();
  dataContext.Objects.DeleteAllOnSubmit(objectsToDelete);
  </code></pre>
  
  <p>solves the "Access to modified closure" message, but leaves the "Value assigned is not used in any execution path" message. That's only solved by removing it altogether:</p>
  
  <pre><code>var objectsToDelete = dataContext.Objects.Where(o =&gt; o.Id == object.Id)).ToList();
  dataContext.Objects.DeleteAllOnSubmit(objectsToDelete);
  </code></pre>