title: Why does Entity Framework 6 generate complex SQL queries for simple lookups?
body: <p>I have this LINQ query</p>
  
  <pre><code>dbContext.Customers.Where(c =&gt; c.AssetTag == assetTag).Count();
  </code></pre>
  
  <p>or</p>
  
  <pre><code>(from c in dbContext.Customers
   where c.AssetTag == assetTag
   select c).Count();
  </code></pre>
  
  <p>The generated SQL is</p>
  
  <pre><code>SELECT 
  [GroupBy1].[A1] AS [C1]
  FROM ( SELECT 
      COUNT(1) AS [A1]
      FROM [dbo].[Customer] AS [Extent1]
      WHERE (([Extent1].[AssetTag] = @p__linq__0) AND ( NOT ([Extent1].[AssetTag] IS NULL    OR @p__linq__0 IS NULL))) OR (([Extent1].[AssetTag] IS NULL) AND (@p__linq__0 IS NULL))
  )  AS [GroupBy1]
  </code></pre>
  
  <p>So why does LINQ generate such complex SQL for a simple where statement?</p>