- Home /
Making if statement inside of findAll
I want to make sth like: ... findAll(x=> if (x. cheese ==true) {x.cheese ! = cheeseCaprice}) To make the ! = cheeseCaprice only if cheese is true
Answer by Bunny83 · Oct 21, 2019 at 02:32 AM
In your specific case you just want to do
findAll(x=> x.cheese == true && !cheeseCaprice);
However lambda expressions can be a full anonymous method when using curly brackets. However in this case you actually have a method body and you have to use the return statement in order to return a value since your method body does not evaluate to a single value anymore.
findAll(x=>{ if (x.cheese == true) return !cheeseCaprice; return false;));
Note that the comparison between x.cheese and cheeseCaprice is pointless since your first condition is that x.cheese is true. That means the additional condition x.cheese ! = cheeseCaprice
would be true ! = cheeseCaprice
which is just the same as cheeseCaprice == false
or !cheeseCaprice
Note: For readability reasons complex lambda expressions / anonymous method should span multiple lines:
findAll(x=>{
if (x.cheese == true)
return !cheeseCaprice;
return false;
));
Answer by unity_4uFVe3pYVuAKtg · Oct 21, 2019 at 01:08 PM
It is a filtered searching system and the cheese tells is in a recipe cheese CheeseCaprice tells when someone dislikes cheese If there isnt cheese and there isnt caprices it would return false so the recipe without cheese would be counted But i want to make it True even if cheese is false. But i think i have and idea for that with loop. BTW can you say me how to do that : recipes[0](0) - first element from list and calling first value from the element by index