簡體   English   中英

刪除約會列表中的特定項目

[英]Removing specific items in an Appointment List

我有使用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);
}

但是,這也會引發錯誤,因為我在遍歷列表時更改了列表的大小。

因此,即使主題可能為空,最好還是刪除主題以“ Canceled:”開頭的最后一個項目中的所有項目。

您可以檢查lambda表達式內的Subject屬性是否為null

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM