无法从列表c中删除某些项目#

时间:2014-08-05 16:34:33

标签: c# list date

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

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

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

我正在尝试删除dateBetween列表中同时位于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;
   }

1 个答案:

答案 0 :(得分:0)

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