如何显示List<;每周工作日>;字符串格式

本文关键字:工作日 每周 gt 字符串 格式 何显示 lt 显示 List | 更新日期: 2023-09-27 18:27:13

这是一个小型应用程序,适用于不同的餐厅以及它们一周中的哪几天营业。在我的界面中,我有一周中每一天的复选框。我想在string.Format中输出每个选定的日期(复选框)。这可能吗?这是抓住复选框的代码:

foreach (CheckBox cb in grpDaysOpen.Controls)   // Add days of week that the restaurant is open
{
    DayOfWeek enumConvertedSuccessfully;        // Will contain good enums soon
    if (cb.Checked && Enum.TryParse<DayOfWeek>(cb.Text, out enumConvertedSuccessfully))
        favoriteDiningPlace.OpenDays.Add(enumConvertedSuccessfully);
}

以下是当前超驰ToString:

return string.Format(
    "{0} is a chian of {1} with a seating capacity of {2}:'r'nSmoking is {3}. Last month's sales were {4}, while last months costs were {5}. 'r'nThe restaurant is open {6}",
    this.Name, 
    this.Chain, 
    this.SeatingCapacity, 
    this.Smoking, 
    this.LastMonthSales, 
    this.LastMonthCosts, 
    this.OpenDays);

我不知道是否需要包含任何其他代码。如果你需要澄清,请告诉我。目前上述ToString输出

"system.collections.generic.list`[system.DayOfWeek]"

对于CCD_ 4。

如何显示List<;每周工作日>;字符串格式

List<DayOfWeek> d = new List<DayOfWeek> { DayOfWeek.Monday, DayOfWeek.Saturday };
string res = string.Join(", ", d);

尝试这个

string days = String.Join(", ",this.OpenDays.Select(x => x.ToString()));

覆盖OpenDays的ToString(),类似于

public class OpenDays
{
   public override ToString()
   {
       StringBuilding formattedOpenDays = new StringBuilder();
       //od is an object of type of single element in OpenDays collection
       foreach(OpenDay od in OpenDays)
       {
           formattedOpenDays.Add(od.ToString());
       }
       return formattedOpenDays.ToString();
   }   
}