c# - 实体框架核心 where 子句额外条件
问题描述
我有这个简单的查询,问题是End
属性是DateTime?
类型,我想包括这个值为空的记录,但我似乎做不到。我尝试使用三元运算符,但仍然没有结果
await _context.Registos
.Where(r => r.Start.Date >= DateStart.Date && r.End.HasValue ? r.End.Value <= DateEnd.Date : !r.End.HasValue )
.AsNoTracking()
.ToListAsync();
解决方案
一种更清洁的方式,如下所示:
await _context.Registos
.Where(r => r.Start.Date >= DateStart.Date
&& (r.End == null || r.End.Value <= DateEnd.Date))
.AsNoTracking()
.ToListAsync();
推荐阅读
- ios - ios/objective c:迭代 UIView.subviews 仅显示部分视图
- system-verilog - 为什么 SystemVerilog 中的 Streaming-Operator 会颠倒字节顺序?
- python - Django信号-同一模型上的多个请求?
- mongodb - 在 docker-compose 中运行 mongo 副本集
- javascript - 如何将输入类型的隐藏值绑定=隐藏到Angular6中的模型
- r - list.file 中的新变量
- python - 无法使用 HTML Scraping 导入数据
- c# - 使用多列 C# 以降序对 DataTable 进行排序
- c - 数组不兼容的指针类型
- angular - 为什么数组引用分配在打字稿中不起作用?