I have two examples of code I wrote. The main idea of this, that I have to params ProtocolNumber
(string) and CreationDate
(DateTime).
In the first piece of code, I tried to connect those two together in an array as a string and after that called string.join. ordered by desc by date
.
What I want to ask: is it ok to twice call CreationDate.ToString()
? Maybe there is a better solution for that. Maybe arrayList is better for multiple data types? Anyways I need to convert dateTime to string.
string[] relatedTaskTemplate = new[] { this.TaskReport.ProtocolNumber, this.TaskReport.CreationDate.ToString()};
string relatedTaskHTML = string.Join(", ", relatedTaskTemplate.OrderByDescending(x => !string.IsNullOrEmpty(TaskReport.CreationDate.ToString(DateTimeFormats.DateTimeFormat))).ToArray());
consultationProtocol = consultationProtocol.Replace("{{ProtocolNumber}}", relatedTaskHTML ?? " ");
This is my second attempt. Using if statement.
string relatedTaskTemplate = !string.IsNullOrEmpty(this.TaskReport.ProtocolNumber)
? ""
: this.TaskReport.ProtocolNumber + " ";
if (!string.IsNullOrEmpty(this.TaskReport.ProtocolNumber) && this.TaskReport.CreationDate.ToString("yy-MM-dd") != " ")
{
relatedTaskTemplate = relatedTaskTemplate.Insert(relatedTaskTemplate.Length, ", ");
}
consultationProtocol = consultationProtocol.Replace("{{ProtocolNumber}}", relatedTaskTemplate ?? " ");
There are some issues with two params connecting together. Once, it's showing only protocolNumber
, other time, only a date. But I need to figure out- if there is a data, that shows a list with them; if no data, then nothing.
Final result displays as html. Like "my code: 1234 01.12.2021, 4321 02.12.2021"