在以下 Linq 查询中,生成的 SQL 忽略 Count() 中的 c.val != null 检查
from t1 in table1
join t2 in table2 on t1.col1 equals t2.col1
where t1.col1 = 123 && t3.Count(c => c.val != null && c.col1 == t1.col1) == 0
select new {t1.col1, t2.col2, t1.col2}
它被翻译成
SELECT [t0].[col1], [t1].[col2], [t0].[col2]
FROM [t1] AS [t0]
INNER JOIN [t2] AS [t1] ON [t0].[col1] = [t1].[col1]
WHERE ([t0].[col1] = @p0) AND (((
SELECT COUNT(*)
FROM [t3] AS [t2]
WHERE [t2].[col1] = [t0].[col1]
)) = @p1)
而当只写以下内容时
t.Count(c => c.ID != null && t.No > 10)
它被翻译成
SELECT COUNT(*) AS [value]
FROM [t] AS [t0]
WHERE ([t0].[ID] IS NOT NULL) AND ([t0].[No] > @p0)
这里它没有跳过 c.ID != null 检查。为什么会出现这种行为?在 where 子句中使用 Count 是否有任何限制?