【发布时间】:2022-01-08 22:15:03
【问题描述】:
我有两个我编写的代码示例。这个的主要思想,我必须参数ProtocolNumber(字符串)和CreationDate(日期时间)。
在第一段代码中,我尝试将这两个字符串连接到一个数组中,然后调用string.join. ordered by desc by date。
我想问的是:可以打两次CreationDate.ToString()吗?也许有更好的解决方案。也许 arrayList 更适合多种数据类型?无论如何,我需要将 dateTime 转换为字符串。
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 ?? " ");
这是我的第二次尝试。使用 if 语句。
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 ?? " ");
两个参数连接在一起存在一些问题。一次,它只显示protocolNumber,其他时候,只显示一个日期。但我需要弄清楚——是否有数据,显示它们的列表;如果没有数据,那么什么都没有。
最终结果显示为 html。比如“我的代码:1234 01.12.2021, 4321 02.12.2021”
【问题讨论】:
-
OrderByDescending(x => !string.IsNullOrEmpty(TaskReport.CreationDate.ToString(DateTimeFormats.DateTimeFormat))).ToArray())你知道你在使用 bool 值吗? -
关于这个帖子stackoverflow.com/questions/13604630/…不知怎么想弄明白的。
标签: c# string xaml replace tostring