Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ query where boolean value is true or false

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)))
like image 242
Nick Avatar asked Oct 16 '11 21:10

Nick


People also ask

Which Linq method returns a Boolean value?

In LINQ, quantifier operators are used to returning a boolean value which shows that whether some or all elements satisfies the given condition.

Do Booleans start as true or false?

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 .

Is Boolean true or 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.

What is the value of true in Boolean?

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.


2 Answers

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)
like image 200
Marcelo Cantos Avatar answered Oct 22 '22 09:10

Marcelo Cantos


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.

like image 4
Ahmad Mageed Avatar answered Oct 22 '22 08:10

Ahmad Mageed