For the following examples we will use DataObject and ListObject classes. The database will be filled up with the FillUpDb method.
If you want to delete all members in a list you can use remove list function.
01private static void RemoveTest(){ 02
IObjectContainer db = Db4oFactory.OpenFile(DbFile); 03
try 04
{ 05
// set update depth to 1 as we only 06
// modify List field 07
db.Ext().Configure().ObjectClass(typeof(ListObject)).UpdateDepth(1); 08
IList<ListObject> result = db.Query<ListObject>(typeof(ListObject)); 09
if (result.Count > 0) 10
{ 11
// retrieve a ListObject 12
ListObject lo1 = result[0]; 13
// remove all the objects from the list 14
lo1.Data.RemoveRange(0, lo1.Data.Count); 15
db.Set(lo1); 16
} 17
} finally { 18
db.Close(); 19
} 20
// check DataObjects in the list 21
// and DataObjects in the database 22
db = Db4oFactory.OpenFile(DbFile); 23
try { 24
IList<ListObject> result = db.Query<ListObject>(typeof(ListObject)); 25
if (result.Count > 0) { 26
ListObject lo1 = result[0]; 27
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count); 28
} 29
IList<DataObject> removedObjects = db.Query<DataObject>(typeof(DataObject)); 30
Console.WriteLine("DataObjects in the database: " + removedObjects.Count); 31
} finally { 32
db.Close(); 33
} 34
}
01Private Shared Sub RemoveTest() 02
Dim db As IObjectContainer = Db4oFactory.OpenFile(DbFile) 03
Try 04
' set update depth to 1 as we only 05
' modify List field 06
db.Ext.Configure.ObjectClass(GetType(ListObject)).UpdateDepth(1) 07
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 08
If result.Count > 0 Then 09
' retrieve a ListObject 10
Dim lo1 As ListObject = result(0) 11
' remove all the objects from the list 12
lo1.Data.RemoveRange(0, lo1.Data.Count) 13
db.Set(lo1) 14
End If 15
Finally 16
db.Close() 17
End Try 18
' check DataObjects in the list 19
' and DataObjects in the database 20
db = Db4oFactory.OpenFile(DbFile) 21
Try 22
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 23
If result.Count > 0 Then 24
Dim lo1 As ListObject = result(0) 25
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count.ToString()) 26
End If 27
Dim removedObjects As IList(Of DataObject) = db.Query(Of DataObject)(GetType(DataObject)) 28
Console.WriteLine("DataObjects in the database: " + removedObjects.Count.ToString()) 29
Finally 30
db.Close() 31
End Try 32
End Sub
However as you will see from the example above, removed objects are not deleted from the database. Here you should be very careful: if you want to delete DataObjects, which were removed from the list you must be sure that they are not referenced by existing objects. Check
Referential Integrity article for more information.
The following example shows how to delete DataObjects from the database as well as from the list:
01private static void RemoveAndDeleteTest() 02
{ 03
IObjectContainer db = Db4oFactory.OpenFile(DbFile); 04
try 05
{ 06
// set update depth to 1 as we only 07
// modify List field 08
db.Ext().Configure().ObjectClass(typeof(ListObject)).UpdateDepth(1); 09
IList<ListObject> result = db.Query<ListObject>(typeof(ListObject)); 10
if (result.Count > 0) 11
{ 12
// retrieve a ListObject 13
ListObject lo1 = result[0]; 14
// create a copy of the objects list 15
// to memorize the objects to be deleted 16
List <DataObject>tempList = new List<DataObject>(lo1.Data); 17
// remove all the objects from the list 18
lo1.Data.RemoveRange(0, lo1.Data.Count); 19
db.Set(lo1); 20
// and delete them from the database 21
for (int i =0; i < tempList.Count; i++) 22
{ 23
db.Delete(tempList[i]); 24
} 25
} 26
} 27
finally 28
{ 29
db.Close(); 30
} 31
// check DataObjects in the list 32
// and DataObjects in the database 33
db = Db4oFactory.OpenFile(DbFile); 34
try 35
{ 36
IList<ListObject> result = db.Query<ListObject>(typeof(ListObject)); 37
if (result.Count > 0) 38
{ 39
ListObject lo1 = result[0]; 40
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count); 41
} 42
IList<DataObject> removedObjects = db.Query<DataObject>(typeof(DataObject)); 43
Console.WriteLine("DataObjects in the database: " + removedObjects.Count); 44
} 45
finally 46
{ 47
db.Close(); 48
} 49
}
01Private Shared Sub RemoveAndDeleteTest() 02
Dim db As IObjectContainer = Db4oFactory.OpenFile(DbFile) 03
Try 04
' set update depth to 1 as we only 05
' modify List field 06
db.Ext.Configure.ObjectClass(GetType(ListObject)).UpdateDepth(1) 07
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 08
If result.Count > 0 Then 09
' retrieve a ListObject 10
Dim lo1 As ListObject = result(0) 11
' create a copy of the objects list 12
' to memorize the objects to be deleted 13
Dim tempList As List(Of DataObject) = New List(Of DataObject)(lo1.Data) 14
' remove all the objects from the list 15
lo1.Data.RemoveRange(0, lo1.Data.Count) 16
db.Set(lo1) 17
' and delete them from the database 18
For i As Integer = 0 To tempList.Count - 1 19
db.Delete(tempList(i)) 20
Next 21
' remove all the objects from the list 22
lo1.Data.RemoveRange(0, lo1.Data.Count) 23
db.Set(lo1) 24
End If 25
Finally 26
db.Close() 27
End Try 28
' check DataObjects in the list 29
' and DataObjects in the database 30
db = Db4oFactory.OpenFile(DbFile) 31
Try 32
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 33
If result.Count > 0 Then 34
Dim lo1 As ListObject = result(0) 35
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count.ToString()) 36
End If 37
Dim removedObjects As IList(Of DataObject) = db.Query(Of DataObject)(GetType(DataObject)) 38
Console.WriteLine("DataObjects in the database: " + removedObjects.Count.ToString()) 39
Finally 40
db.Close() 41
End Try 42
End Sub