Why does Entity Framework 6 generate complex SQL queries for simple lookups?

前端 未结 4 1454
半阙折子戏
半阙折子戏 2020-12-17 01:07

I have this LINQ query

dbContext.Customers.Where(c => c.AssetTag == assetTag).Count();

or

(from c in dbContext.Customers         


        
4条回答
  •  -上瘾入骨i
    2020-12-17 01:23

    That WHERE condition is generated this way because with ANSI NULLS setting, comparing AssetTag == null will not return the corresponding rows in SQL (since in SQL world when comparing null to null the result is null). To keep the query behavior the same as a C# developer would expect, EF generates the extended WHERE clause. Note that previous versions of EF did not do so and thus did not work on databases with ANSI NULLS setting.

    The GroupBy projection is there because EF supports much more complex queries before the .Count() call, such as joins, projections etc. This approach is thus more generic as it will work work all those scenarios as well.

提交回复
热议问题