I have following LINQ query to get a set of data.
var fields = from row in datarows
from field in row
from col in columnnames
where field.Key == col
select new { ColumnName = col, FieldValue = field.Value };
The problem is that my code that handle the fields after this query fails because field.Value
of some rows are returning null
.
My goal is to assign an empty string if null
is detected.
Something like if field.Value == null, then field.Value = ""
Is it possible to do so in linq query?
A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .
'null' is not valid method argument. String. contains() method does not accept 'null' argument. It will throw NullPointerException in case method argument is null.
It will return an empty enumerable. It won't be null.
Use the null coalescing operator ??
:
FieldValue = field.Value ?? ""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With