We warn about this in the "Getting Started" PDF, but it comes up so often, we thought a FAQ was in order.
Do not call MarkAsDeleted within a "foreach" loop.
According to the MSDN docs, you cannot do anything inside a "foreach" that adds, removes, or alters the order of the list.
To delete all entities within a collection, use collection.MarkAllAsDeleted(). It will honor a filter, if one is applied:
Code:
EmployeesCollection collection = new EmployeesCollection();
collection.LoadAll();
collection.Filter = "LastName = 'Doe'";
collection.MarkAllAsDeleted();
collection.Save();
If you need to use more complex criteria than can be done with a filter, then you must loop back wards through the list using the indexer:
C#
Code:
EmployeesCollection collection = new EmployeesCollection();
collection.LoadAll();
for (int i = collection.Count - 1; i >= 0; i--)
{
if (collection[ i ].LastName == "Doe")
{
if (collection[ i ].Age.Value <= 30)
{
collection[ i ].MarkAsDeleted();
}
}
}
collection.Save();
VB.NET
Code:
Dim collection As New EmployeesCollection()
collection.LoadAll()
For i As Integer = collection.Count - 1 To 0 Step -1
If collection(i).LastName = "Doe" Then
If collection(i).Age.Value <= 30 Then
collection(i).MarkAsDeleted()
End If
End If
Next
collection.Save()
David Neal Parsons
www.entityspaces.net