【问题标题】:Make string concatenation with two parameters xaml使用两个参数 xaml 进行字符串连接
【发布时间】: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


【解决方案1】:

我想出了这个解决方案:

                    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 ?? " ");

【讨论】:

    猜你喜欢
    • 2016-09-12
    • 1970-01-01
    • 2012-10-05
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    相关资源
    最近更新 更多