title: How do I fix: Access to foreach variable in closure resharper warning?
body: <p>I'm getting this ReSharper warning: <strong>Access to foreach variable in closure. May have different behaviour when compiled with different versions of compiler</strong>.</p>
  
  <p>This is what I'm doing:</p>
  
  <pre><code>@foreach(var item in Model)
  {
      // Warning underlines "item".
      &lt;div&gt;@Html.DisplayBooleanFor(modelItem =&gt; item.BooleanField)&lt;/div&gt;
  }
  </code></pre>
  
  <p>My extension is as follows:</p>
  
  <pre><code>public static MvcHtmlString DisplayBooleanFor&lt;TModel, TValue&gt;(
      this HtmlHelper&lt;TModel&gt; helper, 
      Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression)
  {
      bool value;
  
      try
      {
          var compiled = expression.Compile()(helper.ViewData.Model);
          value = Convert.ToBoolean(compiled);
      }
      catch (Exception)
      {
          value = false;
      }
  
      return MvcHtmlString.Create(value ? "Yes" : "No");
  }
  </code></pre>
  
  <p>Note this is working as expected but how can I avoid this warning?<br>
  I'll appreciate any help provided.</p>