LINQ query where boolean is true or false

How to compare boolean with true and false in LINQ query?

If hideCompleted is true, I want to show values ​​where IsCompleted is false If hideCompleted is false, I want to show values ​​where IsCompleted is true or false

Example:

(t1.IsCompleted ?? false == (hideCompleted == true ? false : (true || false))) 
+6
source share
3 answers

Just to be sure that I understand you correctly, if hideCompleted is false, do you care what the value of IsCompleted is? If so...

 !(hideCompleted && t1.IsCompleted) 
+6
source

Build your hideCompleted as a true one, similar to this approach:

 var query = dc.SomeTable; if (hideCompleted) { query = query.Where(t1 => !t1.IsCompleted); } 

That way, when hideCompleted true, you filter t1.IsCompleted to false. If hideCompleted false, your original query will capture all results regardless of the value of t1.IsCompleted .

+4
source

you can use this condition

 where (hideCompleted==true && t1.IsCompleted==false) || (hideCompleted==false) 
+3
source

Source: https://habr.com/ru/post/899418/


All Articles