You could check if Subject
property is null
inside your lambda expression:
apts.RemoveAll(apt => apt.Subject != null && apt.Subject.StartsWith("Canceled:"));
Question
I have a list of calendar items that I have retrieved using EWS:
List<Appointment> apts = tApts.Items.ToList<Appointment>();
I am trying to remove all of the appointments in the list that have a subject that contains "Canceled" at the beginning of the title:
apts.RemoveAll(apt => apt.Subject.StartsWith("Canceled:"));
This is throwing me a "Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}
.
I am assuming that this is happening because the Appointment subject is null.
However my solution to this is:
foreach (Appointment apt in apts)
{
if (apt.Subject == null)
continue;
if (apt.Subject.StartsWith("Canceled:"))
apts.Remove(apt);
}
However this also throws an error because I am changing the size of the List while I am iterating through it.
So what would be the best way to remove all the items in the last whose subject starts with "Canceled:", even though the subject may be null.
Solution
You could check if Subject
property is null
inside your lambda expression:
apts.RemoveAll(apt => apt.Subject != null && apt.Subject.StartsWith("Canceled:"));