You have to wrap it in parentheses:
roles.Contains(u.RoleID.Value) && (u.isValid ?? false)
bit of confused with (u.isValid ?? false), does this not mean that if
u.isValid == null then make it false and look for users where
u.isValid is false, this is not what I want.
No, it just means that nulls
are treated as false
and that all users are taken which isValid
is neither null
nor false
. It works because the ??
-operator converts the Nullable<bool>
to a bool
, so you can use it with &&
. I don't like it, i prefer explicit code that i understand later:
roles.Contains(u.RoleID.Value) && u.isValid.HasValue && u.isValid.Value
or simpler by using the ==
-operator with the bool?
:
roles.Contains(u.RoleID.Value) && u.isValid == true