15

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?

7 Answers 7

37

Use the null coalescing operator ??:

FieldValue = field.Value ?? ""
Sign up to request clarification or add additional context in comments.

10 Comments

hey @Jon any specific reason using "" over string.Empty?
@PSL: 10 chars less to type.
Is there a way where I can do this with integers?
@CássioGalvão: Can you be more specific? Value types cannot be null so what exactly do you have in mind?
@Jon, It works with strings, but if 'field.Value' is an integer, I get the message: "Operand '??' cannot be applied to operands of type 'int' and 'int'. How do I set default values of integer types? I'm using linq
|
6
FieldValue = field.Value ?? String.Empty

Comments

4

Use the null-coalescing operator

select new { ColumnName = col, FieldValue = field.Value ?? string.Empty };

The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.

Comments

4

FieldValue = field.Value == null ? "" : field.Value

1 Comment

...all answers better than mine. Learned something.
1

Use ?? operator to return Empty string in case of null

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 ?? string.Empty) };

Comments

0
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 == null ? string.Empty: field.Value};

Comments

0

I also learned that if you are concatenating two fields in a linq field assignment and you are using the null-coalescing operator on only one of the fields, then you need to put parentheses around the field statement as such:

StreetAddr = customer.StreetAddr + ", " + (customer.Suite ?? "")

However, this code is not so great either because if the "Suite" field is null, then I still got that comma-space ", " hanging out after the "StreetAddr" field. Wish I knew a way to fix that?

2 Comments

So this is one way to fix that: StreetAddr = customer.StreetAddr + (string.IsNullOrEmpty(customer.Suite) ? "" : ", " + customer.Suite)
Now some may argue that it's bad to concatenate fields in linq field assignments, and that I should use properties in the view model to concatenate fields instead. I agree, but sometimes I'm lazy and do it anyway. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.