删除约会列表中的特定项目

时间:2014-02-17 20:33:49

标签: c# exchange-server exchangewebservices

我有一个使用EWS检索的日历项目列表:

List<Appointment> apts = tApts.Items.ToList<Appointment>();

我正在尝试删除列表中包含标题开头包含“已取消”主题的所有约会:

apts.RemoveAll(apt => apt.Subject.StartsWith("Canceled:"));

这给我一个"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}

我假设这种情况正在发生,因为约会主题为空。

但我的解决方法是:

foreach (Appointment apt in apts)
{
    if (apt.Subject == null)
        continue;

    if (apt.Subject.StartsWith("Canceled:"))
        apts.Remove(apt);
}

然而,这也会引发错误,因为我正在迭代它时改变List的大小。

那么删除最后一个主题以“已取消:”开头的所有项目的最佳方法是什么,即使主题可能为空。

1 个答案:

答案 0 :(得分:3)

您可以在lambda表达式中检查Subject属性是否为null

apts.RemoveAll(apt => apt.Subject != null && apt.Subject.StartsWith("Canceled:"));