title: LINQ-2-SQL Compiled Queries. Field vs Property?
body: <p>I've recently turned a LINQ-2-SQL function into a Compiled query, and I noticed some really strange behavior with it.  When I store it as a static field, as per every example that I've seen here and on the rest of the web, load testing pegs the CPU at 100% for the entire duration of the test.  When I do it as a static property, on the other hand, the exact same load testing script fluctuates in the 40-100% range.  Why is performance so much better as a property?  Anyone have an idea? As far as I can tell, the only difference between them is that the field takes more CPU than the property.</p>
  
  <p>Here's the two versions:</p>
  
  <pre><code>    private static Func&lt;WebDataContext, int, int, IEnumerable&lt;Product&gt;&gt; GetProductsWithDecendentQuery
      {
          get
          {
              return CompiledQuery.Compile&lt;WebDataContext, int, int, IEnumerable&lt;Product&gt;&gt;((WebDataContext context, int left, int right) =&gt; 
                  context.Taxonomies.Where(x =&gt; x.LeftNumber &gt;= left &amp;&amp; x.RightNumber &lt;= right)
                  .SelectMany(x =&gt; x.InventoryTaxonomies.Select(y =&gt; y.Product))
                  .Distinct().Where(x =&gt; x.Status &amp;&amp; (x.IsDropShip != true || x.Locations.Sum(y =&gt; y.Quantity) &gt; 1))
              );
          }
      }
  </code></pre>
  
  <p>vs </p>
  
  <pre><code>    private static Func&lt;WebDataContext, int, int, IEnumerable&lt;Product&gt;&gt; GetProductsWithDecendentQuery =
          CompiledQuery.Compile&lt;WebDataContext, int, int, IEnumerable&lt;Product&gt;&gt;((WebDataContext context, int left, int right) =&gt; 
                  context.Taxonomies.Where(x =&gt; x.LeftNumber &gt;= left &amp;&amp; x.RightNumber &lt;= right)
                  .SelectMany(x =&gt; x.InventoryTaxonomies.Select(y =&gt; y.Product))
                  .Distinct().Where(x =&gt; x.Status &amp;&amp; (x.IsDropShip != true || x.Locations.Sum(y =&gt; y.Quantity) &gt; 1))
              );
  </code></pre>