Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return empty string if object null

Tags:

c#

linq

The object i is from database. PrDT is a string, PrDateTime is DataTimeOffset type, nullable

vi.PrDT = i.PrDateTime.Value.ToString("s");

What is the quick way? I don't want if else etc...


1 Answers

Using the conditional operator:

vi.PrDT = i.PrDateTime.HasValue ? i.PrDateTime.Value.ToString("s") :
                                  string.Empty;
like image 195
Oded Avatar answered Dec 19 '25 21:12

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!