在从列表中删除某些项时遇到麻烦

本文关键字:遇到 麻烦 列表 删除 | 更新日期: 2023-09-27 18:16:43

因此,我正在构建一个会议调度程序,其中每个潜在参与者都有一组不可用的日期。为了便于测试,我插入了我自己的。

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

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

我试图删除存在于datesBetween列表中也在unavailableDates列表中的所有日期,只留下不在人员不可用日期上的日期。

我已经尝试了一些不同的东西,但我似乎不能使它工作。

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;
   }

在从列表中删除某些项时遇到麻烦

似乎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();