c# - Lambda 在 bool where 子句中将 null 视为 false
问题描述
我有以下实体框架 lambda 查询:
public IList<MyClass> GetMyClass(int id, bool show)
{
using (var ctx = new DbContext())
{
return ctx.MyClasses.Where(x =>
x.Id == id &&
x.Show == show // <-- nullable bool
.OrderByDescending(x => x.CreationDate).Take(100).ToList();
}
}
我的前端视图传递了show
布尔值,指示用户对返回内容的偏好。
在数据库中,show
属性是nullable
。
这是一个非常繁重的查询,所以我一次将其限制为 100 个,数千行是null
,数千行是 ,数千行true
是false
。
问题
我怎么能说,而不使查询效率低下,伪代码:
.Where(x => x.Show == show) (where null or false == false)
就目前而言,如果我传递False
下去,则将排除空值,并且我需要将它们归类为 False。
我无法更改数据库
解决方案
怎么样
(x.Show ?? false) == show
推荐阅读
- doit - 并行运行时等待依赖
- javascript - 从头开始创建 toUpperCase 函数时的问题
- c - c中的sizeof运算符对相同类型的指针在C中给出不同的结果
- kubernetes - Openshift vs Rancher,有什么区别?
- php - 指定为定义者的用户('mysql.infoschema'@'localhost')不存在 - 第一个 Laravel 项目
- c# - 插入 URL 的变量未显示在地址栏中
- python-3.x - Python 程序 ValueError:以 10 为底的 int() 的无效文字:
- php - 发布方法不适用于 IIS 重写规则
- flutter - 来自 FutureBuilder 的 Navigator.pop
- javascript - 克隆 chrome CTRL + F 实现