In some cases you may find it necessary to use Evaluations. There is no standard sorting API for the Evaluation results. But you can sort the returned result set using standard Java collection API.
For example, let's retrieve the objects of the Pilot class saved before, selecting only pilots with even points and sorting them according to their name:
01private static void GetObjectsEval() 02
{ 03
IObjectContainer db = Db4oFactory.OpenFile(Db4oFileName); 04
try 05
{ 06
DateTime dt1 = DateTime.UtcNow; 07
IQuery query = db.Query(); 08
query.Constrain(typeof(Pilot)); 09
query.Constrain(new PilotEvaluation()); 10
ArrayList result = new ArrayList(query.Execute()); 11
result.Sort(new PilotComparer()); 12
DateTime dt2 = DateTime.UtcNow; 13
TimeSpan diff = dt2 - dt1; 14
Console.WriteLine("Time to execute with Evaluation query and collection sorting: " + diff.TotalMilliseconds + " ms."); 15
ListResult(result); 16
} 17
finally 18
{ 19
db.Close(); 20
} 21
}
1private class PilotEvaluation : IEvaluation 2
{ 3
public void Evaluate(ICandidate candidate) 4
{ 5
Pilot pilot = (Pilot)candidate.GetObject(); 6
candidate.Include(pilot.Points % 2 == 0); 7
} 8
}
01private class PilotComparer : IComparer 02
{ 03
public int Compare(object p1, object p2) 04
{ 05
if (p1 is Pilot && p2 is Pilot) 06
{ 07
Pilot pilot1 = (Pilot)p1; 08
Pilot pilot2 = (Pilot)p2; 09
return pilot1.Name.CompareTo(pilot2.Name); 10
} 11
return 0; 12
} 13
}
01Private Shared Sub GetObjectsEval() 02
Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 03
Try 04
Dim dt1 As DateTime = DateTime.UtcNow 05
Dim query As IQuery = db.Query 06
query.Constrain(GetType(Pilot)) 07
query.Constrain(New PilotEvaluation) 08
Dim result As ArrayList = New ArrayList(query.Execute) 09
result.Sort(New PilotComparer) 10
Dim dt2 As DateTime = DateTime.UtcNow 11
Dim diff As TimeSpan = dt2 - dt1 12
Console.WriteLine("Time to execute with Evaluation query and collection sorting: " + diff.TotalMilliseconds.ToString() + " ms.") 13
ListResult(result) 14
Finally 15
db.Close() 16
End Try 17
End Sub
1Private Class PilotEvaluation 2
Implements IEvaluation 3
4
Public Sub Evaluate(ByVal candidate As ICandidate) Implements IEvaluation.Evaluate 5
Dim pilot As Pilot = CType(candidate.GetObject, Pilot) 6
candidate.Include(pilot.Points Mod 2 = 0) 7
End Sub 8
End Class
01Private Class PilotComparer 02
Implements IComparer 03
Public Function Compare(ByVal p1 As Object, ByVal p2 As Object) As Integer Implements IComparer.Compare 04
If TypeOf p1 Is Pilot AndAlso TypeOf p2 Is Pilot Then 05
Dim pilot1 As Pilot = CType(p1, Pilot) 06
Dim pilot2 As Pilot = CType(p2, Pilot) 07
Return pilot1.Name.CompareTo(pilot2.Name) 08
End If 09
Return 0 10
End Function 11
End Class
This sorting method can be used to sort query results when the sorting can not be added to the query (Evaluations, QBE).