我是Entity Framework和SQL的新手。我想知道哪一个(一点点)更快:Any()
或FirstOrDefault()
。请参阅下面我将如何使用它们。
选项1
if(Comments.Select(c=>c.ObjectId).FirstOrDefault()>0) {...}
生成SQL
SELECT TOP (1)
[c].[ObjectId] AS [ObjectId]
FROM [dbo].[Comments] AS [c]
选项2
if(Comments.Select(c=>c.ObjectId).Any()) {...}
生成SQL
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Comments] AS [Extent1]
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Comments] AS [Extent2]
)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]