I'm getting DBNull results from DB.
Trying to apply ?? operator to it like
result["field1"] ?? result["field2"]
field1 = DBNull
field2 = 4
but it doesn't work, returns {} because result["field1"] is not null (it's DBNull). I expect to get that 4 from it.
Tried to do
result["field1"] = null
first, but it doesn't work, it's still DBNull type.
The question is how to handle this, convert DBNull to null in some way? How to make ?? operator work with DBNull values?
To be more precise:
Is there a way to get COALESCE-like behaviour?
That 2 fields are just for example, in reality there will be much more fields and I'm trying to get first not null (so I was hoping to use chaining field1 ?? field2 ?? field3...)
I wasn't precise with that, sorry, my fault.
SOLUTION
Following Peter van der Heijden solution, say I'm getting from DB
result["field1"] = DBNull
result["field2"]= DBNull
result["field3"] = 4
result["field1"] ?? result["field2"] ?? result["field3"]
will return {} (first not null, DBNull is ... well ... not null)
but
result["field1"] as int? ?? result["field2"] as int? ?? result["field3"] as int?
will return result["field3"] = 4, as expected.
int? can be replaced with whatever result type you get (like DateTime? for example).