Entity Framework 4生成的COUNT SQL语句问题

在ADO.NET Entity Framework 4中,查询记录数的LINQ to Entities语句生成的SQL语句竟然包含子查询。

>>>使用Entity Framework 4的查询代码:

using (SpaceObjectContext context = new SpaceObjectContext())
{
return context.SiteMsgs.Count(msg => msg.SenderSpaceUserId == 1);
}

>>>生成的SQL语句:

SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[club_Message] AS [Extent1]
WHERE 1 = [Extent1].[SenderID]
)
AS [GroupBy1]

LINQ to SQL却不存在这个问题:

>>>使用LINQ to SQL的查询:

using(club_CNBlogsDataContext context = new club_CNBlogsDataContext())
{
return context.club_Messages.Count(msg => msg.SenderID == 1);
}

>>>生成的SQL语句:

exec sp_executesql N'SELECT COUNT(*) AS [value]
FROM [dbo].[club_Message] AS [t0]
WHERE [t0].[SenderID] = @p0
',N'@p0 int',@p0=1
posted @ 2011-01-27 11:24  dudu  阅读(6207)  评论(6编辑  收藏  举报