1

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"

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    `OrderByDescending(x => !string.IsNullOrEmpty(TaskReport.CreationDate.ToString(DateTimeFormats.DateTimeFormat))).ToArray())` Are you aware that you are odering on a bool value? – Jeroen van Langen Dec 02 '21 at 12:11
  • regarding to this post https://stackoverflow.com/questions/13604630/c-sharp-linq-orderby-filtering-null-or-empty-values-to-be-last Somehow tried to figure it out. –  Dec 02 '21 at 12:47

1 Answers1

0

I came up with this solution:

                    string joinedString = string.Empty;

                    foreach (var task in this.TaskReport.RelatedTaskList
                        .GroupBy((task) =>
                            new
                            {
                                task.ProtocolNumber,
                                task.CreationDate
                            })
                        .Select((group) => group.First())
                        .OrderBy((task) => task.ProtocolNumber)
                        .ThenByDescending((task) => task.CreationDate))
                    {
                        joinedString += relatedtasks.Replace(
                            "{{RelatedTaskProtocolNumberDate}}",
                            string.Join(
                                " ",
                                new string[]
                                {
                                    task.ProtocolNumber,
                                    task.CreationDate.ToString(DateTimeFormats.DateFormat)
                                }.Where((item) => !item.IsNullOrEmptyOrWhiteSpace()))
                               );  
                    }
                    consultationProtocol = consultationProtocol.Replace("{{RelatedTaskProtocolNumberDate}}",joinedString ?? " ");