Monday, 15 April 2013

c# - Operator '&&' cannot be applied to operands of type 'bool' and 'bool?' -



c# - Operator '&&' cannot be applied to operands of type 'bool' and 'bool?' -

this question has reply here:

weird operator precedence ?? (null coalescing operator) 2 answers

trying read datacontext class

var users = new list<user>(); var roles = new int[] { 1, 2 }; // here need users who's role in role (1, 2), hence above line // , need user who's isvalid field true, please note isvalid sql 'bit' field , nullable //this doing foreach (var user in datacontext.users .where(u => roles.contains(u.roleid.value) && u.isvalid ?? false == true)) // here '&&' part i'm struggling , getting above error { users.add(new user() { // users added in collection }); }

so question in clause need user's in role(1,2) && isvalid == true, if isvalid 'null' create false.

you have wrap in parentheses:

roles.contains(u.roleid.value) && (u.isvalid ?? false)

bit of confused (u.isvalid ?? false), not mean if u.isvalid == null create false , users u.isvalid false, not want.

roles.contains(u.roleid.value) && u.isvalid.hasvalue && u.isvalid

c# linq

No comments:

Post a Comment