views:

1019

answers:

7

I am pulling varchar values out of a DB and want to set the string I am assigning them to as "" if they are null. I'm currently doing it like this.

if (IsNullOrEmpty(planRec.approved_by) == true)
  this.approved_by = "";
else
  this.approved_by = planRec.approved_by.toString();

There seems like there should be a way to do this in a single line something like

this.approved_by = "" || planRec.approved_by.toString();

However I can't find an optimal way to do this. Is there a better way or is what I have the best way to do it?

Thanks

+11  A: 

Try this:

this.approved_by = IsNullOrEmpty(planRec.approved_by) ? "" : planRec.approved_by.toString();

You can also use the null-coalescing operator as other have said - since no one has given an example that works with your code here is one:

this.approved_by = planRec.approved_by ?? planRec.approved_by.toString();

But this example only works since a possible value for this.approved_by is the same as one of the potential values that you wish to set it to. For all other cases you will need to use the conditional operator as I showed in my first example.

Andrew Hare
Just a side note, this is referred to as the null coalescing operator.
R. Bemrose
I did post the null coalesce operator first but I think the conditional operator is better suited to what the OP is asking.
Andrew Hare
@R. Bemrose...the null coalescing operator is ??...see @Dave's answer below.
Paul Alexander
@Paul - I did use the ?? at first and then edited my answer to what you see above.
Andrew Hare
Yeah, when I made that comment, it was still showing the ?? version. I noticed it had changed when I reloaded the page to see what other answers were submitted.
R. Bemrose
@Andrew...ahh...guess this would be a good time for "Edit comments" to correct that.
Paul Alexander
@Paul - Yes, I agree!
Andrew Hare
+7  A: 

The coalesce operator (??) is what you want, I believe.

Dave Ward
Yep...but only because the default is "".
Paul Alexander
+2  A: 

You are looking for the C# coalesce operator: ??. This operator takes a left and right argument. If the left hand side of the operator is null or a nullable with no value it will return the right argument. Otherwise it will return the left.

var x = somePossiblyNullValue ?? valueIfNull;
JaredPar
Oops, I was a bit late there. Good answer.
Yohnny
This will not actually work for my application because the .ToString will throw in error
Splashlin
A: 

Removed, due to duplicate answer

Yohnny
This won't work if `approved_by == null`.
Dmitri Nesteruk
+4  A: 

My guess is the best you can come up with is

this.approved_by = IsNullOrEmpty(planRec.approved_by) ? string.Empty
                                                      : planRec.approved_by.ToString();

Of course since you're hinting at the fact that approved_by is an object (which cannot equal ""), this would be rewritten as

this.approved_by = (planRec.approved_by ?? string.Empty).ToString();
Dmitri Nesteruk
+1 for most complete answer. If I could give another +1 for using string.Empty, I would.
Steven Sudit
+3  A: 

To extend @Dave's answer...if planRec.approved_by is already a string

this.approved_by = planRec.approved_by ?? "";
Paul Alexander
A: 

u can also do it in ur query..for instance in sql server, google ISNULL and CASE builtin functions