c# - 返回记录的方法 true false 或 all
问题描述
有没有更好的方法来编写下面的方法?我想根据bool
变量返回内容IsPublished
。
我什么时候想返回既是IsPublished
和的条目。null
true
false
public IList<Content> GetContent(bool? IsPublished)
{
if (IsPublished != null)
{
return _UoW.Content.All.Where(c => c.IsPublished == IsPublished).ToList();
}
else
{
return _UoW.Content.All.ToList();
}
}
解决方案
如果更好,你的意思是更短,运营商?
可以成为解决方案。此外,当isPublished == null
. 请参阅此处的 Microsoft 文档以了解?
操作员。
public IList<Content> GetContent(bool? IsPublished)
{
return isPublished != null ? _UoW.Content.All.Where(c=>c.IsPublished== IsPublished).ToList() : _UoW.Content.All.ToList();
}
推荐阅读
- react-native - 使用 lottie.js 给出错误 TypeError: Attempted to assign to readonly property
- azure - Azure 服务总线无法连接到 kubernetes docker
- node.js - 使用 puppeteer 打印大 HTML 页面的最后一页
- rest - REST API 返回带有 200 状态码的空响应
- python - How to plot bars such that for value less than a threshold value are in an opposite direction than the ones greater?
- github - 在 graphql 查询中区分私有/内部 github 存储库
- uncrustify - 如何使用 uncrustify 对齐函数声明?
- asp.net-core - IIS 标头太长
- android - 使用 Firebase 等第三方库的 Android AOSP 构建错误 - “appname”取决于未定义的模块“com.google.firebase_firebase-core”
- django - save() 缺少 1 个必需的位置参数:'self'