3

I'm reading back a nullable DateTime? property then assigning that value to a string property in short date format.

I can convert the date time value to a short date string and assign to the IT_Date_String property. But I'm not sure how to assign a "" value to the string if the IT_Date is null.

How can you convert a datetime? value to string.empty when datetime? is null?

This is the assignment in linq:

var status_list = query_all.ToList().Select(r => new RelStatus
{
    IT_Date_String = r.IT_Date.Value.ToString("yyyy-MM-dd") != null ? r.IT_Date.Value : null
}).ToList();

And the properties in the model:

public DateTime? IT_Date { get; set; }
public string IT_Date_String { get; set; }
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
Brian Var
  • 6,029
  • 25
  • 114
  • 212

4 Answers4

11

You're calling the IT_Date.Value.ToString(...) regardless of whether IT_Date actually has a value.

So you need to turn the expression around:

r.IT_Date.HasValue ? r.IT_Date.Value.ToString(...) : ""

This way ToString() will only be called when IT_Date has a value.

You can also implement this in the getter, as mentioned in a now-deleted comment:

public string IT_Date_String 
{ 
    get
    {
        return IT_Date.HasValue ? IT_Date.Value.ToString(...) : "";
    }
}

This way you won't have to reimplement the logic everywhere you access this model, and as a bonus, it will only be executed when it's actually requested.

There's also no need to explicitly use String.Empty, the string "" will be interned to the same at runtime.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I like the idea of injecting the conversion into the model property. But when I did that the assignment tells me `IT_Date_String ` is read only. Do I need to add a setter also to that string property? – Brian Var Sep 28 '16 at 12:54
  • Like: `IT_Date_String = r.IT_Date` says that assignment is read only. – Brian Var Sep 28 '16 at 12:58
  • If you want to be able to set a date by assigning a string to the `IT_Date_String` property, then yes, you also need a setter that converts the string to a DateTime and assigns that to the `IT_Date` property. – CodeCaster Sep 28 '16 at 13:21
8

In C# 6 you can do this:

IT_Date_String = r.IT_Date?.ToString("yyyy-MM-dd") ?? String.Empty;

The new ? checks if the thing on the left is null, if it is, the expression evaluates to null. If not, it just continues the evaluation.

Then, ?? checks whether the result of the first expression is null, which it would be if IT_Date is null. If it is, evaluate to String.Empty.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
3

With C# 6.0 and null propagation you can use:

IT_Date_String = r.IT_Date?.ToString("yyyy-MM-dd") ?? String.Empty
decPL
  • 5,384
  • 1
  • 26
  • 36
1

This one will work in any version of the framework:

IT_Date_String=string.Format("{0:yyyy-MM-dd}",IT_Date);
cyberhubert
  • 203
  • 1
  • 9
  • True, though `yyyy-MM-dd` might be more valid, at least using common sense understanding of date/time tracking. :) – decPL Sep 28 '16 at 13:47
  • 1
    Thanks for pointing this, I have corrected my answer. Probably it will be better to use CultureInfo instead of raw pattern, but that was out of the question scope – cyberhubert Sep 28 '16 at 14:13