Home > Mobile >  Parenthesis in Where expression not working
Parenthesis in Where expression not working

Time:07-29

I have been trying to make an expression using parenthesis in a where, but is not working.

This is an example of the expression:

 var data = context.table.Where(a => !string.IsNullOrEmpty(a.IdName) && (a.type == "Iphone" && a.number != 12)).ToList();

Does the && is affecting in something?

CodePudding user response:

Seems like your logic is flawed. You apparently need items where IdName is not null or empty:

!string.IsNullOrEmpty(a.IdName)

But you also want to exclude any Iphone with number 12, so you want:

(a.type != "Iphone" || a.number != 12)

(either type is different from Iphone or number is different from 12, so it can't be both Iphone and 12)

Which leads to:

!string.IsNullOrEmpty(a.IdName) && (a.type != "Iphone" || a.number != 12)

CodePudding user response:

try this

var data = context.table.Where(a => (!string.IsNullOrEmpty(a.IdName)) && (a.type == "Iphone" && a.number != 12)).ToList();
  • Related