c# - Return data based on condition in Linq C#
问题描述
I have below query but it throws error
An exception was thrown while attempting to evaluate a LINQ query parameter expression
int? status = null;
BankDBContext.AccountDBs.Where(x => x.IsActive == (!status.HasValue? x.IsActive : (status.Value == 1))).ToList();
I want to return all records if status is null and if status has 1 return active records else in active records.
Thanks.
解决方案
如果为 null,则返回所有内容,如果为 1,则返回活动项,否则为非活动项
BankDBContext.AccountDBs.Where(x => status == null || x.IsActive == (status == 1))
推荐阅读
- arrays - 一维阵列中的最小移动算法以匹配另一个阵列的位置
- c# - 将项目添加到只有一个项目大小不同的 wpf 列表框?
- neo4j - Neo4j 中的顺序事件建模
- amazon-web-services - Boto3 向非默认 VPC 中的安全组添加入站规则
- excel - 如何选择工作表上的特定单元格?
- python - Python FPDF如何在2列中显示并且在创建新页面时不拆分地址
- swift - 表视图未同步
- html - .NET Core MVC 中嵌套表单的解决方法
- javascript - 将 node-postgres 结果放入回调函数之外的对象中
- python-3.x - 处理烧瓶应用程序中的 API 和 UI 错误