0

I have this linq query

Dim chiamateAperte = From statoRic In
                                    dbVulcano.StatoRic.Where(Function(s) s.RFStato >= 11 And s.RFStato <= 13 And s.Attuale = 1 And s.RFTecnico = rfTecnico)
                                 From richiesta In
                                    dbVulcano.Richieste.Where(Function(r) r.IDRic = statoRic.RFRic).DefaultIfEmpty()
                                 From cliente In
                                    dbVulcano.Clienti.Where(Function(c) c.IDCliente = richiesta.RFCliente).DefaultIfEmpty()
                                 Select statoRic.ID, statoRic.RFRic, statoRic.RFStato, statoRic.Attuale, richiesta.Descr, cliente.RagSociale, statoRic.DataAss, statoRic.Data, dataf = If(statoRic.DataAss.HasValue, statoRic.DataAss, statoRic.Data), statoRic.OraDalle, statoRic.OraAlle
                                 Order By dataf Descending, statoRic.OraDalle Ascending

It's working fine but I want to add an order condition on "OraDalle" field. OraDalle it's sort of a time field, but sadly on db it's defined as smallInt (and I cannot change it), so the format is like "800" to say "eight o'clock". This field can be null. So instead of statoRic.OraDalle I tried this:

orad=if (statoRic.OraDalle.HasValue,statoRic.OraDalle,2359)

And then

Order By dataf Descending, orad Ascending

But it throws an ugly error: cannot cast system nullable 1 to system object of type 'system.nullable'. Only primitive types or enumeration types are supported in this context

Then I tried this:

orad=statoRic.OraDalle.GetValueOrDefault(2359)

But it also throw an error like: linq to entities does not recognize the method 'Int16 GetValueOrDefault(Int16)'

So... how can I achieve what I want? This whole mess is to get a list ordered by time, where null time values are at the bottom (and not on the top, as default). Thank you all!

1 Answers1

0

Instead of if (statoRic.OraDalle.HasValue,statoRic.OraDalle,2359) use if (statoRic.OraDalle,2359)

This is the equivalent of C#´s null-coalescing-operator ??: statoRic.OraDalle ?? 2359 which was the solution for a similar question.

Community
  • 1
  • 1
Alex B.
  • 2,145
  • 1
  • 16
  • 24