How can I compare a boolean value to both true and false in a 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)))
In LINQ, quantifier operators are used to returning a boolean value which shows that whether some or all elements satisfies the given condition.
The default value of Boolean is False . Boolean values are not stored as numbers, and the stored values are not intended to be equivalent to numbers. You should never write code that relies on equivalent numeric values for True and False .
A Boolean variable has only two possible values: true or false. It is common to use Booleans with control statements to determine the flow of a program.
Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0. The following table shows comparisons and boolean operations. This is true if x is false, and false if x is true.
Just to be sure I understand you correctly, if hideCompleted is false, you don't care what the value of IsCompleted is? If so...
!(hideCompleted && t1.IsCompleted)
Build your query based on hideCompleted
being true, similar to this approach:
var query = dc.SomeTable;
if (hideCompleted)
{
query = query.Where(t1 => !t1.IsCompleted);
}
This way when hideCompleted
is true you filter for t1.IsCompleted
being false. When hideCompleted
is false your original query will grab all results regardless of the value of t1.IsCompleted
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With