c# - 检查参数值是否在查询中为空
问题描述
在我的查询中,我基于RoleId
and获取记录LocationId
,有时用户可能不会传递位置,在这种情况下我想删除该过滤器并从所有位置获取信息。
目前我正在这样做
if(loc > 0)
{
var myResult = (from x in CSDB.Allocations
join s in CSDB.Managers
on x.ManagerId equals s.Id
Where x.RoleId == 2 && s.LocationId == loc
select new
{
x.name,
x.Date
}).ToList();
}
else
{
var myResult = (from x in CSDB.Allocations
join s in CSDB.Managers
on x.ManagerId equals s.Id
Where x.RoleId == 2
select new
{
x.name,
x.Date
}).ToList();
}
我正在查看是否可以检查查询中是否loc
为空,而不是使用 if else。
解决方案
你可以这样做:
Where x.RoleId == 2 && (loc == null || s.LocationId == loc)
推荐阅读
- sql - 如何实现对亚马逊 Redshift 的原始 sql 访问的行级安全性 (RLS)?
- linux - 面临shell脚本中的文件夹路径问题
- android - 如何将数据从对话框发送到我的活动 kotlin?
- javascript - 如何在 Jquery /Js 中匹配 2 个数组的值
- list - 列表中的python类型错误
- xaringan - 增量内容在 div 内不起作用
- javascript - 使用折叠时组件本地状态未获取状态
- python - 类型错误:__init__() 缺少 1 个必需的位置参数:“单位”
- angular - 如何使用 docker 在前端部署 angular 并在后端部署烧瓶的微服务?
- xampp - 在 localhost 上安装新的 TYPO3:后端不起作用