如果下面的查询中objHistory.IsLoginFromWhitelistedIP
为'true',我想将t1.Role.IsAvailableFromRestrictedIp
与 true和false 进行比较并返回所有内容。
return from t1 in db.UserRoles
where ((t1.Role.IsAvailableFromRestrictedIp
== objHistory.IsLoginFromWhitelistedIP)
select t1;
实现这一目标的最佳方式是什么?
答案 0 :(得分:3)
from t1 in db.UserRoles
where !objHistory.IsLoginFromWhitelistedIP || (t1.Role.IsAvailableFromRestrictedIp ?? true)
select t1
或
from t1 in db.UserRoles
where objHistory.IsLoginFromWhitelistedIP && (t1.Role.IsAvailableFromRestrictedIp ?? true)
select t1
取决于objHistory.IsLoginFromWhitelistedIP
的过滤函数,意思是:
objHistory.IsLoginFromWhitelistedIP
值为false
=该记录可能有效,具体取决于t1.Role.IsAvailableFromRestrictedIp
:采取版本1 objHistory.IsLoginFromWhitelistedIP
值为false
=该记录无效(无需对t1.Role.IsAvailableFromRestrictedIp
进行评估):采取版本2 此外,您可以在System.Nullable<bool>
后指定??
的默认值,或者转到.GetValueOrDefault(T defaultValue)
答案 1 :(得分:1)
return from t1 in db.UserRoles
where t1.Role.IsAvailableFromRestrictedIp != null &&
objHistory.IsLoginFromWhitelistedIP
select t1;