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
// set update depth to 1 as we only 03
// modify List field 04
IConfiguration configuration = Db4oFactory.NewConfiguration(); 05
configuration.ObjectClass(typeof(ListObject)).UpdateDepth(1); 06
IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 07
try 08
{ 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
// remove all the objects from the list 15
lo1.Data.RemoveRange(0, lo1.Data.Count); 16
db.Set(lo1); 17
} 18
} finally { 19
db.Close(); 20
} 21
// check DataObjects in the list 22
// and DataObjects in the database 23
db = Db4oFactory.OpenFile(Db4oFileName); 24
try { 25
IList<ListObject> result = db.Query<ListObject>(typeof(ListObject)); 26
if (result.Count > 0) { 27
ListObject lo1 = result[0]; 28
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count); 29
} 30
IList<DataObject> removedObjects = db.Query<DataObject>(typeof(DataObject)); 31
Console.WriteLine("DataObjects in the database: " + removedObjects.Count); 32
} finally { 33
db.Close(); 34
} 35
}
01Private Shared Sub RemoveTest() 02
' set update depth to 1 as we only 03
' modify List field 04
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 05
configuration.ObjectClass(GetType(ListObject)).UpdateDepth(1) 06
Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 07
Try 08
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 09
If result.Count > 0 Then 10
' retrieve a ListObject 11
Dim lo1 As ListObject = result(0) 12
' remove all the objects from the list 13
lo1.Data.RemoveRange(0, lo1.Data.Count) 14
db.Set(lo1) 15
End If 16
Finally 17
db.Close() 18
End Try 19
' check DataObjects in the list 20
' and DataObjects in the database 21
db = Db4oFactory.OpenFile(Db4oFileName) 22
Try 23
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 24
If result.Count > 0 Then 25
Dim lo1 As ListObject = result(0) 26
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count.ToString()) 27
End If 28
Dim removedObjects As IList(Of DataObject) = db.Query(Of DataObject)(GetType(DataObject)) 29
Console.WriteLine("DataObjects in the database: " + removedObjects.Count.ToString()) 30
Finally 31
db.Close() 32
End Try 33
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
// set update depth to 1 as we only 04
// modify List field 05
IConfiguration configuration = Db4oFactory.NewConfiguration(); 06
configuration.ObjectClass(typeof(ListObject)).UpdateDepth(1); 07
IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 08
try 09
{ 10
IList<ListObject> result = db.Query<ListObject>(typeof(ListObject)); 11
if (result.Count > 0) 12
{ 13
// retrieve a ListObject 14
ListObject lo1 = result[0]; 15
// create a copy of the objects list 16
// to memorize the objects to be deleted 17
List <DataObject>tempList = new List<DataObject>(lo1.Data); 18
// remove all the objects from the list 19
lo1.Data.RemoveRange(0, lo1.Data.Count); 20
db.Set(lo1); 21
// and delete them from the database 22
for (int i =0; i < tempList.Count; i++) 23
{ 24
db.Delete(tempList[i]); 25
} 26
} 27
} 28
finally 29
{ 30
db.Close(); 31
} 32
// check DataObjects in the list 33
// and DataObjects in the database 34
db = Db4oFactory.OpenFile(Db4oFileName); 35
try 36
{ 37
IList<ListObject> result = db.Query<ListObject>(typeof(ListObject)); 38
if (result.Count > 0) 39
{ 40
ListObject lo1 = result[0]; 41
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count); 42
} 43
IList<DataObject> removedObjects = db.Query<DataObject>(typeof(DataObject)); 44
Console.WriteLine("DataObjects in the database: " + removedObjects.Count); 45
} 46
finally 47
{ 48
db.Close(); 49
} 50
}
01Private Shared Sub RemoveAndDeleteTest() 02
' set update depth to 1 as we only 03
' modify List field 04
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 05
configuration.ObjectClass(GetType(ListObject)).UpdateDepth(1) 06
Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 07
Try 08
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 09
If result.Count > 0 Then 10
' retrieve a ListObject 11
Dim lo1 As ListObject = result(0) 12
' create a copy of the objects list 13
' to memorize the objects to be deleted 14
Dim tempList As List(Of DataObject) = New List(Of DataObject)(lo1.Data) 15
' remove all the objects from the list 16
lo1.Data.RemoveRange(0, lo1.Data.Count) 17
db.Set(lo1) 18
' and delete them from the database 19
For i As Integer = 0 To tempList.Count - 1 20
db.Delete(tempList(i)) 21
Next 22
' remove all the objects from the list 23
lo1.Data.RemoveRange(0, lo1.Data.Count) 24
db.Set(lo1) 25
End If 26
Finally 27
db.Close() 28
End Try 29
' check DataObjects in the list 30
' and DataObjects in the database 31
db = Db4oFactory.OpenFile(Db4oFileName) 32
Try 33
Dim result As IList(Of ListObject) = db.Query(Of ListObject)(GetType(ListObject)) 34
If result.Count > 0 Then 35
Dim lo1 As ListObject = result(0) 36
Console.WriteLine("DataObjects in the list: " + lo1.Data.Count.ToString()) 37
End If 38
Dim removedObjects As IList(Of DataObject) = db.Query(Of DataObject)(GetType(DataObject)) 39
Console.WriteLine("DataObjects in the database: " + removedObjects.Count.ToString()) 40
Finally 41
db.Close() 42
End Try 43
End Sub