I have seen it done like this:
db.Table.Where(
item => item.IsApproved.HasValue == isApproved.HasValue &&
(!item.IsApproved.HasValue || item.IsApproved.Value==isApproved.Value )
).Count();
Well, I had this problem before, I remember that the problem is in converting the LINQ query to a SQL statement.
The second expression has an equal in SQL that: Where IsAproved is null
but the first expression does not because it is a comparision between a value in the database with a C# nullable variable.
To solve it, I would suggest to try:
db.Table.Where(item => isApproved != null ? item.IsApproved == isApproved.Value
: item.IsApproved == null).Count();
I don't know about the performance hit, but it works
bool? isApproved = null;
db.Table.Where(item => item.IsApproved == isApproved ||
!(item.IsApproved.HasValue || isApproved.HasValue))
.Count();
Try:
db.Table.Where(item => item.IsApproved == isApproved.Value).Count();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.