【问题标题】:Having trouble removing certain items from list c#从列表 c# 中删除某些项目时遇到问题
【发布时间】:2014-08-05 16:34:33
【问题描述】:

所以我正在构建一个会议安排程序,其中每个潜在参与者都有一组不可用的日期。为了测试,我插入了自己的。

用户输入一个日期范围,然后将其放入中间所有日期的列表中。

我目前有一个不可用日期列表,具体取决于用户邀请谁参加活动。这被保存到一个名为 availableDates 的列表中。

我正在尝试删除 datesBetween 列表中存在的所有日期,这些日期也在availableDates 列表中,只留下不在某人不可用日期上的日期。

我尝试了一些不同的方法,但似乎无法正常工作。

private void compileDates()
    {
        DateTime startingDate = cal1.SelectionEnd;                                                  //Get the starting date from the first calender
        DateTime endingDate = cal2.SelectionEnd;                                                    //Get the ending date from the first calender

        var namesList = lstConfirmedParticipants.Items.Cast<String>().ToList();                     //Collects all the participants names and converts it to a list

        List<DateTime> PhillChambers = new List<DateTime>();                                        //Set Pauls Unavailable Dates
        PhillChambers.Add(new DateTime(2014, 08, 11));
        PhillChambers.Add(new DateTime(2014, 08, 12));
        PhillChambers.Add(new DateTime(2014, 08, 17));

        List<DateTime> HenryWright = new List<DateTime>();                                          //Set Pauls Unavailable Dates
        HenryWright.Add(new DateTime(2014, 08, 09));
        HenryWright.Add(new DateTime(2014, 08, 12));
        HenryWright.Add(new DateTime(2014, 08, 14));

        List<DateTime> PaulaCooper = new List<DateTime>();                                          //Set Pauls Unavailable Dates
        PaulaCooper.Add(new DateTime(2014, 08, 11));
        PaulaCooper.Add(new DateTime(2014, 08, 12));
        PaulaCooper.Add(new DateTime(2014, 08, 16));

        List<DateTime> unavailableDates = new List<DateTime>();                                     //Creates a new list to hold all the unavailable dates            

        if (namesList.Contains("Paula Cooper"))
        {                                                                                           //Add Paulas Unavailable Dates
            unavailableDates.AddRange(PaulaCooper);
        }
        if (namesList.Contains("Henry Wright"))
        {                                                                                           //Add Henrys Unavailable Dates
            unavailableDates.AddRange(HenryWright);
        }
        if (namesList.Contains("Phill Chambers"))
        {                                                                                           //Add Phills Unavailable Dates
            unavailableDates.AddRange(PhillChambers);
        }

        foreach (DateTime date in GetDateRange(startingDate, endingDate))
        {
            lstDatesBetween.Items.Add(date.ToShortDateString());                                    //Get all the dates between the date ranges and put them into the listbox.
        }

        List<DateTime> datesBetween = lstDatesBetween.Items.OfType<DateTime>().ToList();            //Convert the Listbox into a list holding all the dates between the date ranges

        datesBetween.RemoveAll(item => unavailableDates.Contains(item));                            //remove all the dates in dates between that also appear in unavailable dates

        List<DateTime> availableDates = new List<DateTime>();                                       //Creates a new list to hold all the available dates(FUTURE USE)

        availableDates.AddRange (datesBetween); 

        lstDatesAvailable.DataSource = availableDates;                                              //display the available dates for the meeting




private List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
   {
       if (StartingDate > EndingDate)
       {
           return null;
       }
       List<DateTime> datesBetween = new List<DateTime>();
       DateTime tempDate = StartingDate;
       do
       {
           datesBetween.Add(tempDate);
           tempDate = tempDate.AddDays(1);
       } while (tempDate <= EndingDate);
       return datesBetween;
   }

【问题讨论】:

  • 调试器说什么?

标签: c# list date


【解决方案1】:

lstDatesBetween 似乎填充了字符串:

lstDatesBetween.Items.Add(date.ToShortDateString());

然后datesBetween 采用DateTime 类型的元素:

List<DateTime> datesBetween = lstDatesBetween.Items.OfType<DateTime>().ToList();

所以我猜datesBetween 是空的。

试试这是否可行:

List<DateTime> datesBetween = lstDatesBetween.Items
                                  .OfType<string>()
                                  .Select(s => Convert.ToDateTime(s))
                                  .ToList();

【讨论】:

  • 是的,我现在看到了,我很困惑,因为我之前显示了日期。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 2013-05-28
  • 2012-08-18
  • 2021-12-08
  • 2021-02-04
  • 2016-06-24
相关资源
最近更新 更多