我正在过滤 IQueryable 以返回所有将字段 UserId(可为 null 的 int)设置为 null 的实体。查询生成不正确的 SQL 因而失败——语句如下——
var filtered = certificates.Where(c => !c.UserId.HasValue).Select(c => c.SubjectName);
生成的 SQL 是——
SELECT
CAST(NULL AS varchar(1)) AS [C1],
CAST(NULL AS int) AS [C2],
CAST(NULL AS datetime2) AS [C3],
CAST(NULL AS datetime2) AS [C4],
CAST(NULL AS bit) AS [C5],
CAST(NULL AS datetime2) AS [C6],
CAST(NULL AS int) AS [C7]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
WHERE 1 = 0
知道 WTF 正在进行吗?这个想法很简单,我只想返回字段 UserId 为 false 的所有行。UserId 可以为空,并且被查询的表有三行符合所描述的条件,但是 LINQ 查询返回 0。
谢谢!