0
votes

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?

2
Define 'not working'. Does it not compile? Does it give the wrong answer? Does it reset your computer? Also, maybe show the data you are using, and what you think the answer should be - Neil
Is not letting other data to filter. The whole idea is that I can see every data with the IdName is not empty or null and not see every IPhone that is 12. Anything else I should be able to see. - darksaky
Can you create a complete example (including data) that demonstrates the problem? - David L
no can do, that's why is an example but the problem is how the where is stated. - darksaky
Seems like per your comment that you need || (OR) instead of && (AND) - Rafalon

2 Answers

1
votes

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)
0
votes

try this

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