首页 > 解决方案 > 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.

标签: c#entity-frameworklinq

解决方案


如果为 null,则返回所有内容,如果为 1,则返回活动项,否则为非活动项

BankDBContext.AccountDBs.Where(x => status == null || x.IsActive == (status == 1))

推荐阅读