This group of examples shows how to select ranges of objects. Store Pilots function is used to fill in the database.
Select "Test" pilots with the points range of more than 6.
01private static void SelectTestPilots6PointsMore() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
IList<Pilot> result = container.Query<Pilot>(delegate(Pilot pilot) 09
{ 10
// all Pilots containing "Test" in the name 11
// and 6 point are included in the result 12
bool b1 = pilot.Name.IndexOf("Test") >= 0; 13
bool b2 = pilot.Points > 6; 14
return b1 && b2; 15
16
}); 17
ListResult(result); 18
} 19
catch (Exception ex) 20
{ 21
System.Console.WriteLine("System Exception: " + ex.Message); 22
} 23
finally 24
{ 25
CloseDatabase(); 26
} 27
} 28
}
01Private Shared Sub SelectTestPilots6PointsMore() 02
Dim container As IObjectContainer = Database() 03
If Not container Is Nothing Then 04
Try 05
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf TestPilots6PointsMoreMatch) 06
ListResult(result) 07
Catch ex As Exception 08
System.Console.WriteLine("System Exception: " + ex.Message) 09
Finally 10
CloseDatabase() 11
End Try 12
End If 13
End Sub
1Private Shared Function TestPilots6PointsMoreMatch(ByVal p As Pilot) As Boolean 2
' all Pilots containing "Test" in the name 3
' and 6 point are included in the result 4
Dim b1 As Boolean = p.Name.IndexOf("Test") >= 0 5
Dim b2 As Boolean = p.Points > 6 6
Return b1 AndAlso b2 7
End Function
Select all pilots, who have points in [6,12] range.
01private static void SelectPilots6To12Points() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
IList<Pilot> result = container.Query<Pilot>(delegate(Pilot pilot) 09
{ 10
// all Pilots having 6 to 12 point are 11
// included in the result 12
return ((pilot.Points >= 6) && (pilot.Points <= 12)); 13
14
}); 15
ListResult(result); 16
} 17
catch (Exception ex) 18
{ 19
System.Console.WriteLine("System Exception: " + ex.Message); 20
} 21
finally 22
{ 23
CloseDatabase(); 24
} 25
} 26
}
01Private Shared Sub SelectPilots6To12Points() 02
Dim container As IObjectContainer = Database() 03
If Not container Is Nothing Then 04
Try 05
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf Pilots6To12PointsMatch) 06
ListResult(result) 07
Catch ex As Exception 08
System.Console.WriteLine("System Exception: " + ex.Message) 09
Finally 10
CloseDatabase() 11
End Try 12
End If 13
End Sub
1Private Shared Function Pilots6To12PointsMatch(ByVal p As Pilot) As Boolean 2
' all Pilots having 6 to 12 point are 3
' included in the result 4
Return ((p.Points >= 6) AndAlso (p.Points <= 12)) 5
End Function
Select pilots randomly: random array of point values is generated, those pilots who have points values within this array are included in the result set.
01private static void SelectPilotsRandom() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
IObjectSet result = container.Query(new RandomPredicate()); 09
ListResult(result); 10
} 11
catch (Exception ex) 12
{ 13
System.Console.WriteLine("System Exception: " + ex.Message); 14
} 15
finally 16
{ 17
CloseDatabase(); 18
} 19
} 20
}
01private class RandomPredicate : Db4objects.Db4o.Query.Predicate 02
{ 03
private IList randomArray = null; 04
05
private IList GetRandomArray() 06
{ 07
if (randomArray == null) 08
{ 09
Random rand = new Random(); 10
randomArray = new ArrayList(); 11
for (int i = 0; i < 10; i++) 12
{ 13
int random = (int)(rand.Next(10)); 14
randomArray.Add(random); 15
} 16
} 17
return randomArray; 18
} 19
20
public bool Match(Pilot pilot) 21
{ 22
// all Pilots having points in the values of 23
// the randomArray 24
return GetRandomArray().Contains(pilot.Points); 25
} 26
}
01Private Shared Sub SelectPilotsRandom() 02
Dim container As IObjectContainer = Database() 03
If Not container Is Nothing Then 04
Try 05
Dim result As IObjectSet = container.Query(New RandomPredicate()) 06
ListResult(result) 07
Catch ex As Exception 08
System.Console.WriteLine("System Exception: " + ex.Message) 09
Finally 10
CloseDatabase() 11
End Try 12
End If 13
End Sub
[code=nqcollectionVB.zip#SimpleExamples.vb@ RandomPredicate]
Select pilots with even points.
01private static void SelectPilotsEven() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
IList<Pilot> result = container.Query<Pilot>(delegate(Pilot pilot) 09
{ 10
// all Pilots having even points 11
return pilot.Points % 2 == 0; 12
}); 13
ListResult(result); 14
} 15
catch (Exception ex) 16
{ 17
System.Console.WriteLine("System Exception: " + ex.Message); 18
} 19
finally 20
{ 21
CloseDatabase(); 22
} 23
} 24
}
01Private Shared Sub SelectPilotsEven() 02
Dim container As IObjectContainer = Database() 03
If Not container Is Nothing Then 04
Try 05
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf PilotsEvenMatch) 06
ListResult(result) 07
Catch ex As Exception 08
System.Console.WriteLine("System Exception: " + ex.Message) 09
Finally 10
CloseDatabase() 11
End Try 12
End If 13
End Sub
1Private Shared Function PilotsEvenMatch(ByVal p As Pilot) As Boolean 2
' all Pilots having even points 3
Return p.Points Mod 2 = 0 4
End Function
Select one pilot and quit.
01private static void SelectAnyOnePilot() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
IObjectSet result = container.Query(new AnyPilotPredicate()); 09
SimpleExamples.ListResult(result); 10
} 11
catch (Exception ex) 12
{ 13
System.Console.WriteLine("System Exception: " + ex.Message); 14
} 15
finally 16
{ 17
CloseDatabase(); 18
} 19
} 20
}
1
01Private Shared Sub SelectAnyOnePilot() 02
Dim container As IObjectContainer = Database() 03
If Not container Is Nothing Then 04
Try 05
Dim result As IObjectSet = container.Query(New AnyPilotPredicate()) 06
SimpleExamples.ListResult(result) 07
Catch ex As Exception 08
System.Console.WriteLine("System Exception: " + ex.Message) 09
Finally 10
CloseDatabase() 11
End Try 12
End If 13
End Sub
1
01private static void StoreDuplicates() 02
{ 03
File.Delete(Db4oFileName); 04
IObjectContainer container = Database(); 05
if (container != null) 06
{ 07
try 08
{ 09
Pilot pilot; 10
for (int i = 0; i < ObjectCount; i++) 11
{ 12
pilot = new Pilot("Test Pilot #" + i, i); 13
container.Set(pilot); 14
} 15
for (int i = 0; i < ObjectCount; i++) 16
{ 17
pilot = new Pilot("Test Pilot #" + i, i); 18
container.Set(pilot); 19
} 20
container.Commit(); 21
} 22
catch (Db4oException ex) 23
{ 24
System.Console.WriteLine("Db4o Exception: " + ex.Message); 25
} 26
catch (Exception ex) 27
{ 28
System.Console.WriteLine("System Exception: " + ex.Message); 29
} 30
finally 31
{ 32
CloseDatabase(); 33
} 34
} 35
}
01private static void SelectDistinctPilots() 02
{ 03
IObjectContainer container = Database(); 04
if (container != null) 05
{ 06
try 07
{ 08
DistinctPilotsPredicate predicate = new DistinctPilotsPredicate(); 09
IObjectSet result = container.Query(predicate); 10
ListResult(predicate.uniqueResult); 11
} 12
catch (Exception ex) 13
{ 14
System.Console.WriteLine("System Exception: " + ex.Message); 15
} 16
finally 17
{ 18
CloseDatabase(); 19
} 20
} 21
}
01private class DistinctPilotsPredicate : Predicate 02
{ 03
public Dictionary<Pilot, object> uniqueResult = new Dictionary<Pilot, object>(); 04
05
public bool Match(Pilot pilot) 06
{ 07
// each Pilot is included in the result 08
uniqueResult.Add(pilot, null); 09
return false; 10
} 11
}
01Private Shared Sub StoreDuplicates() 02
File.Delete(Db4oFileName) 03
Dim container As IObjectContainer = Database() 04
If Not container Is Nothing Then 05
Try 06
Dim pilot As Pilot 07
Dim i As Integer 08
For i = 0 To ObjectCount - 1 Step i + 1 09
pilot = New Pilot("Test Pilot #" + i.ToString(), i) 10
container.Set(pilot) 11
Next 12
i = 0 13
For i = 0 To ObjectCount - 1 Step i + 1 14
pilot = New Pilot("Test Pilot #" + i.ToString(), i) 15
container.Set(pilot) 16
Next 17
container.Commit() 18
Catch ex As Db4oException 19
System.Console.WriteLine("Db4o Exception: " + ex.Message) 20
Catch ex As Exception 21
System.Console.WriteLine("System Exception: " + ex.Message) 22
Finally 23
CloseDatabase() 24
End Try 25
End If 26
End Sub
01Private Shared Sub SelectDistinctPilots() 02
Dim container As IObjectContainer = Database() 03
If Not container Is Nothing Then 04
Try 05
Dim predicate As DistinctPilotsPredicate = New DistinctPilotsPredicate() 06
Dim result As IObjectSet = container.Query(predicate) 07
ListResult(predicate.uniqueResult) 08
Catch ex As Exception 09
System.Console.WriteLine("System Exception: " + ex.Message) 10
Finally 11
CloseDatabase() 12
End Try 13
End If 14
End Sub
01Private Class DistinctPilotsPredicate 02
Inherits Predicate 03
04
Public uniqueResult As Dictionary(Of Pilot, Object) = New Dictionary(Of Pilot, Object)() 05
06
Public Function Match(ByVal p As Pilot) As Boolean 07
' each Pilot is included in the result 08
uniqueResult.Add(p, Nothing) 09
Return False 10
End Function 11
End Class