在Entity Framework中,哪个更快Any()或FirstOrDefault()?

时间:2013-05-22 08:33:03

标签: c# .net entity-framework-5

我是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]

0 个答案:

没有答案