为什么在申请时没有返回null值!=在linq中

Pab*_*ind 2 c# linq asp.net entity-framework

我用这个代码

var items2 = from item in context.Images
             where item.Reported != true
             select item;
Run Code Online (Sandbox Code Playgroud)

但是在"报告"列中没有返回空值为什么?

Jus*_*son 8

true或者false与...不一样的价值null.如果您需要返回两者,那么您需要将查询更改为:

var items2 = from item in context.Images
             where item.Reported != true || item.Reported == null
             select item;
Run Code Online (Sandbox Code Playgroud)

  • 在C#中,`null!= true`计算为`true` (2认同)

gho*_*ord 7

实体框架在将您的查询转换为SQL时,会生成类似于的代码

select * from images
where reported <> true
Run Code Online (Sandbox Code Playgroud)

在null值上使用的SQL中的任何运算符都返回false(null <> true也是),这就是为什么在结果集中没有获得具有报告空值的图像的原因.