There are multiple places in an Open Source Project (OSP) code I contribute, where it has to be determined if an element in a collection satisfies a certain condition.
We can use .Count(x => x....) != 0 instead of using .Any(x => x....) or .FirstOrDefault(x => x....) != null
Because query generation of Linq is below,
(In Any(), the 2nd(Not Exists) condition is not need i think.)
.Any(x => x.Col_1 == 'xxx')
(@p__linq__0 varchar(8000))SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Table_X] AS [Extent1]
WHERE [Extent1].[Col_1] = @p__linq__0
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Table_X] AS [Extent2]
WHERE [Extent2].[Col_1] = @p__linq__0
)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
.FirstOrDefault(x => x.Col_1 == 'xxx') != null
(@p__linq__0 varchar(8000))SELECT TOP (1)
[Extent1].[Col_1] AS [Col_1],
[Extent1].[Col_2] AS [Col_2],
...
[Extent1].[Col_n] AS [Col_n]
FROM [dbo].[Table_X] AS [Extent1]
WHERE [Extent1].[Col_1] = @p__linq__0
.Count(x => x.Col_1 == 'xxx') != 0
(@p__linq__0 varchar(8000))SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Table_X] AS [Extent1]
WHERE [Extent1].[Col_1] = @p__linq__0
) AS [GroupBy1]