在我的项目中,我使用 Entity Framework 4.4.0.0 并且遇到以下困境。我必须检查用户是否被激活。我的查询看起来像:
任何()
_context.Users.Any(u => u.Id == userId && u.IsActivated);
生成的sql为:
SELECT CASE
WHEN ( EXISTS (SELECT 1 AS [C1]
FROM [dbo].[Users] AS [Extent1]
WHERE ( [Extent1].[Id] = @p__linq__0 )
AND ( [Extent1].[IsActivated] = 1 )) ) THEN cast(1 AS BIT)
WHEN ( NOT EXISTS (SELECT 1 AS [C1]
FROM [dbo].[Users] AS [Extent2]
WHERE ( [Extent2].[Id] = @p__linq__0 )
AND ( [Extent2].[IsActivated] = 1 )) ) THEN cast(0 AS BIT)
END AS [C1]
FROM (SELECT 1 AS X) AS [SingleRowTable1]
因为Count()
我得到这个查询:
SELECT [GroupBy1].[A1] AS [C1]
FROM (SELECT COUNT(1) AS [A1]
FROM [dbo].[Users] AS [Extent1]
WHERE ( [Extent1].[Id] = @p__linq__0 )
AND ( [Extent1].[IsActivated] = 1 )) AS [GroupBy1]
这看起来对吗?我不像 sql 那样好……但对我来说它看起来效率不高。我错过了什么吗?
' select count(*) from dbo.Users where id=@id and IsActivated=1
' 效率低吗?