Home > OS >  Why putting OR with three boolean type columns would randomly fetch or sometimes not fetch the data?
Why putting OR with three boolean type columns would randomly fetch or sometimes not fetch the data?

Time:09-27

I have a table

Students

now students has 3 Booleans checks

1. IsRecordArchived
2. IsActive
3. IsOnProbation

The Booleans are not relevant to each other and doesn't depend on each other but the record should not appear or included in the result set if any of it is TRUE.

e.g.

A student can be IsRecordArchived = TRUE but might not be the other two i.e. IsActive= false and IsOnProbation = false or could be IsActive= True and IsOnProbation= false or true even.

Or it could all be equal to TRUE. Could be anything but ultimately it should not be included in the record.

Now, my query

var students= db.Students.where(s=> !IsActive || !IsRecordArchived || !IsOnProbation).ToList();

Would sometimes work but sometimes not. How do I adjust these?

e.g.

studnet records

ID  Name  IsArchived  IsDeleted IsOnProbation
1   Tom       1          0           0
2   Dick      1          1           0
3   Harry     0          0           1
4.  Amas      0          0           0

Now, according to my query; only 4. Amas should be in the result set not the others.

CodePudding user response:

the record should not appear or included in the result set if any of it is TRUE.

Does this really implement that logic?

var students= db.Students.where(s=> !IsActive || !IsRecordArchived || IsOnProbation).ToList();

That's going to get records where either of IsActive and IsRecordArchived is false or IsOnProbation is true.

If you want records where none are true, i.e. all are false, then you need to specify that all are false and use AND operators:

var students= db.Students.Where(s=> !IsActive && !IsRecordArchived && !IsOnProbation).ToList();
  • Related