Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading