Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# || operator not working with nullable booleans

I have the following piece of code in my LINQ:

    where (tf.Shipped || tf.Ordered || tf.Processed) 

Note that Shipped, Ordered and Processed are all nullable Boolean fields

I am getting the following message:

Operator || cannot be applied to operands of type 'bool?' and 'bool?'

Not sure how to resolve this as yes, they need to be nullable booleans and I need to use the OR (||).

like image 762
Nate Pet Avatar asked Jan 27 '12 17:01

Nate Pet


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


2 Answers

Take a step back and think about the problem. You want a collection of widgets where the widget was ordered, or the widget was shipped, or the widget was processed.

There are four possible states for your knowledge of "ordered":

  • this widget was ordered and I know that (true)
  • this widget was not ordered and I know that (false)
  • this widget was ordered but I don't know that (null)
  • this widget was not ordered but I don't know that (null)

There are four states but only three values possible values. Therefore if "ordered" is in the null state you do not know whether it should be included in the query results or not.

The compiler doesn't know that either.

There simply is not enough information available for the compiler to give you a query that has the semantics you want. The compiler is not going to make a guess and possibly give you bad results; the compiler is going to tell you that there's not enough information here and you need to do more work to make the query unambiguous.

What you have to do is say what to do in the case where you don't know the answer. The query "all the widgets that were ordered, shipped or processed" is impossible because some widgets we don't know whether they were ordered, shipped or processed, and so we don't know whether to include them or not. But the query "all the widgets that I know were ordered, or that I know were shipped, or that I know were processed" is a query that the compiler can make sense of:

where (tf.Shipped ?? false) || (tf.Ordered ?? false) || (tf.Processed ?? false) 

That means "if I don't know whether it was shipped, etc, assume it was not".

You might instead want the query "all the widgets that definitely were, or might have been shipped, ordered or processed:

where (tf.Shipped ?? true) || (tf.Ordered ?? true) || (tf.Processed ?? true) 

The compiler isn't going to guess which side you want to err on when there is insufficient information to give accurate results; the compiler might guess wrong and we're not in the business of making decisions on your behalf. You're going to have to make that decision.

like image 176
Eric Lippert Avatar answered Oct 10 '22 02:10

Eric Lippert


Try

 where (tf.Shipped == true || tf.Ordered  == true || tf.Processed == true ) 
like image 33
500 - Internal Server Error Avatar answered Oct 10 '22 00:10

500 - Internal Server Error