实体sql count null

时间:2013-07-20 13:55:38

标签: sql entity-framework-5 entity-sql

我在使用Entity SQL计算空值时遇到了麻烦。

SQL中的代码:

SELECT Table.City, COUNT(*)
FROM Table
GROUP BY Table.City

和Linq to Entities:

var query1 = from g in context.Table
             group g by g.City into mygroup
             select new { mygroup, c = mygroup.Count() };

都返回正确的值:

NULL    4
Boston  7
Canberra    1
London  5
Melbourne   5
New York    4
Paris   15
Sydney  4

但是,实体SQL代码编写如下:

string queryString1 = "SELECT city, COUNT(g.City)" +
                      "FROM Entities.Table AS g " +
                      "GROUP BY g.City as city";

返回:

NULL    0
Boston  7
Canberra    1
London  5
Melbourne   5
New York    4
Paris   15
Sydney  4

为什么NULL计数不正确?我该如何解决?

1 个答案:

答案 0 :(得分:0)

How Entity SQL Differs from Transact-SQL

  

实体SQL不支持count(*)聚合。请改用count(0)。

通常COUNT(*)会对行进行计数,而COUNT(g.City)会为您提供NOT NULL值。