Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime? to string

Tags:

c#

I need to convert a Datetime? to a string. The ?? operator dosent work and calling tostring on a null would make things fall over. Any advice on how to handle datetime? Thanks

Below is my code for context. o.CustomerRequiredDate is a Datetime? and PredictedActivationDate is a string. Both need to stay as the datatype they currently are.

 {
       rsp = (from r in db.btRequests
                                      join o in db.NewOrders
                                      on r.ID equals o.RequestId
                                      join s in db.Status
                                      on r.Status equals s.ID.ToString()
                                      select new DataLayer.OrderResponse
                                      {
                                          RequestID = requestID,
                                          Message = s.StatusDescription,
                                          OrderStatus = r.Status.ToString(),
                                          PredictedActivationDate =r.Status == "3" || r.Status == "4" || r.Status == "5"
                                          || r.Status == "6" || r.Status == "9" ? (o.CustomerRequiredDate ?? ""): ""

                                      }).FirstOrDefault();
like image 791
Tom Squires Avatar asked Jul 01 '26 19:07

Tom Squires


1 Answers

You could do

o.CustomerRequiredDate.HasValue ? o.CustomerRequiredDate.Value.ToString() : ""
like image 182
Samuel Béliveau Avatar answered Jul 04 '26 05:07

Samuel Béliveau