Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if nullable boolean is not true?

Righ now I have this that works, but its ugly and long:

        var details = dc.SunriseShipment
            .Where(it => (it.isDeleted == null || it.isDeleted == false));

Is there a better way to do this? I tried "it.isDeleted != true" and "it.isDeleted ?? false == false" but they are not working.

like image 214
kovacs lorand Avatar asked Mar 20 '15 13:03

kovacs lorand


People also ask

How do you know if a boolean value is true?

To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean.

IS null boolean false C#?

The way you typically represent a “missing” or “invalid” value in C# is to use the “null” value of the type. Every reference type has a “null” value; that is, the reference that does not actually refer to anything.

How do I check if a boolean is null or empty in C#?

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.

What is a nullable boolean?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.


2 Answers

Try this:

.Where(it => !(it.isDeleted ?? false));
like image 171
Giorgos Betsos Avatar answered Sep 19 '22 00:09

Giorgos Betsos


There is an GetValueOrDefault method which returns a default value when the value is null:

var details = dc.SunriseShipment
.Where(it => !it.isDeleted.GetValueOrDefault(false));
like image 44
MarkO Avatar answered Sep 21 '22 00:09

MarkO