I have this LINQ query
dbContext.Customers.Where(c => c.AssetTag == assetTag).Count();
or
(from c in dbContext.Customers
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.