Calculation Examples

This set of examples shows how to use Native Queries to perform different calculations on the objects in the database. Store Pilots function is used to fill in the database.

SumPilotPoints

Calculate the sum of the points of all the pilots in the database. 

CalculationExamples.cs: SumPilotPoints
01private static void SumPilotPoints() 02 { 03 IObjectContainer container = Database(); 04 05 if (container != null) 06 { 07 try 08 { 09 SumPredicate sumPredicate = new SumPredicate(); 10 IObjectSet result = container.Query(sumPredicate); 11 ListResult(result); 12 System.Console.WriteLine("Sum of pilots points: " + sumPredicate.sum); 13 } 14 catch (Exception ex) 15 { 16 System.Console.WriteLine("System Exception: " + ex.Message); 17 } 18 finally 19 { 20 CloseDatabase(); 21 } 22 } 23 }
CalculationExamples.cs: SumPredicate
01private class SumPredicate : Predicate 02 { 03 public int sum = 0; 04 05 public bool Match(Pilot pilot) 06 { 07 // return all pilots 08 sum += pilot.Points; 09 return true; 10 } 11 }
CalculationExamples.vb: SumPilotPoints
01Private Shared Sub SumPilotPoints() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim sumPredicate As SumPredicate = New SumPredicate() 06 Dim result As IObjectSet = container.Query(sumPredicate) 07 ListResult(result) 08 System.Console.WriteLine("Sum of pilots points: " + sumPredicate.sum.ToString()) 09 Catch ex As Exception 10 System.Console.WriteLine("System Exception: " + ex.Message) 11 Finally 12 CloseDatabase() 13 End Try 14 End If 15 End Sub
CalculationExamples.vb: SumPredicate
01Private Class SumPredicate 02 Inherits Query.Predicate 03 04 Public sum As Integer = 0 05 06 Public Function Match(ByVal p As Pilot) As Boolean 07 ' return all pilots 08 sum += p.Points 09 Return True 10 End Function 11 End Class

SelectMinPointsPilot

Find a pilot having the minimum points. 

CalculationExamples.cs: SelectMinPointsPilot
01private static void SelectMinPointsPilot() 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 // return all pilots 11 return true; 12 13 }, new System.Comparison<Pilot>(delegate(Pilot p1, Pilot p2) 14 { 15 // sort by points then by name 16 return p1.Points - p2.Points; 17 } 18 )); 19 if (result.Count > 0) 20 { 21 System.Console.WriteLine("The min points result is: " 22 + result[0]); 23 } 24 } 25 catch (Exception ex) 26 { 27 System.Console.WriteLine("System Exception: " + ex.Message); 28 } 29 finally 30 { 31 CloseDatabase(); 32 } 33 } 34 }
CalculationExamples.vb: SelectMinPointsPilot
01Private Shared Sub SelectMinPointsPilot() 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 AllPilotsMatch, New System.Comparison(Of Pilot)(AddressOf PilotPointsCompare)) 06 If result.Count > 0 Then 07 System.Console.WriteLine("The min points result is: " + result(0).ToString()) 08 End If 09 Catch ex As Exception 10 System.Console.WriteLine("System Exception: " + ex.Message) 11 Finally 12 CloseDatabase() 13 End Try 14 End If 15 End Sub
CalculationExamples.vb: AllPilotsMatch
1Private Shared Function AllPilotsMatch(Of Pilot)(ByVal p As Pilot) As Boolean 2 ' return all pilots 3 Return True 4 End Function
CalculationExamples.vb: PilotPointsCompare
1Private Shared Function PilotPointsCompare(ByVal p1 As Pilot, ByVal p2 As Pilot) As Integer 2 ' sort by points then by name 3 Return p1.Points - p2.Points 4 End Function

AveragePilotPoints

Calculate what is the average amount of points for all the pilots in the database. 

CalculationExamples.cs: AveragePilotPoints
01private static void AveragePilotPoints() 02 { 03 IObjectContainer container = Database(); 04 05 if (container != null) 06 { 07 try 08 { 09 AveragePredicate averagePredicate = new AveragePredicate(); 10 IObjectSet result = container.Query(averagePredicate); 11 if (averagePredicate.count > 0) 12 { 13 System.Console.WriteLine("Average points for professional pilots: " 14 + averagePredicate.sum 15 / averagePredicate.count); 16 } 17 else 18 { 19 System.Console.WriteLine("No results"); 20 } 21 } 22 catch (Exception ex) 23 { 24 System.Console.WriteLine("System Exception: " + ex.Message); 25 } 26 finally 27 { 28 CloseDatabase(); 29 } 30 } 31 }
CalculationExamples.cs: AveragePredicate
01private class AveragePredicate : Predicate 02 { 03 public int sum = 0; 04 05 public int count = 0; 06 07 public bool Match(Pilot pilot) 08 { 09 // return professional pilots 10 if (pilot.Name.StartsWith("Professional")) 11 { 12 sum += pilot.Points; 13 count++; 14 return true; 15 } 16 return false; 17 } 18 }
CalculationExamples.vb: AveragePilotPoints
01Private Shared Sub AveragePilotPoints() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim averagePredicate As AveragePredicate = New AveragePredicate() 06 Dim result As IObjectSet = container.Query(averagePredicate) 07 If averagePredicate.count > 0 Then 08 System.Console.WriteLine("Average points for professional pilots: " _ 09 + (averagePredicate.sum / averagePredicate.count).ToString()) 10 Else 11 System.Console.WriteLine("No results") 12 End If 13 Catch ex As Exception 14 System.Console.WriteLine("System Exception: " + ex.Message) 15 Finally 16 CloseDatabase() 17 End Try 18 End If 19 End Sub
CalculationExamples.vb: AveragePredicate
01Private Class AveragePredicate 02 Inherits Query.Predicate 03 Public sum As Integer = 0 04 05 Public count As Integer = 0 06 07 Public Function Match(ByVal p As Pilot) As Boolean 08 ' return professional pilots 09 If p.Name.StartsWith("Professional") Then 10 sum = sum + p.Points 11 count = count + 1 12 Return True 13 End If 14 Return False 15 End Function 16 End Class

CountSubGroups

Calculate how many pilots are in each group ("Test", "Professional").

CalculationExamples.cs: CountSubGroups
01private static void CountSubGroups() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 CountPredicate predicate = new CountPredicate(); 09 IObjectSet result = container.Query(predicate); 10 ListResult(result); 11 IDictionaryEnumerator enumerator = predicate.countTable.GetEnumerator(); 12 while (enumerator.MoveNext()) 13 { 14 System.Console.WriteLine(enumerator.Key + ": " + enumerator.Value); 15 } 16 } 17 catch (Exception ex) 18 { 19 System.Console.WriteLine("System Exception: " + ex.Message); 20 } 21 finally 22 { 23 CloseDatabase(); 24 } 25 } 26 }
CalculationExamples.cs: CountPredicate
01private class CountPredicate : Predicate 02 { 03 04 public Hashtable countTable = new Hashtable(); 05 06 public bool Match(Pilot pilot) 07 { 08 // return all Professional and Test pilots and count in 09 // each category 10 String[] keywords = { "Professional", "Test" }; 11 foreach (string keyword in keywords) 12 { 13 if (pilot.Name.StartsWith(keyword)) 14 { 15 if (countTable.ContainsKey(keyword)) 16 { 17 countTable[keyword] = ((int)countTable[keyword]) + 1; 18 } 19 else 20 { 21 countTable.Add(keyword, 1); 22 } 23 return true; 24 } 25 } 26 return false; 27 } 28 }
CalculationExamples.vb: CountSubGroups
01Private Shared Sub CountSubGroups() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim predicate As CountPredicate = New CountPredicate() 06 Dim result As IObjectSet = container.Query(predicate) 07 ListResult(result) 08 Dim enumerator As IDictionaryEnumerator = predicate.countTable.GetEnumerator() 09 While enumerator.MoveNext() 10 System.Console.WriteLine(enumerator.Key.ToString() + ": " + enumerator.Value.ToString()) 11 End While 12 Catch ex As Exception 13 System.Console.WriteLine("System Exception: " + ex.Message) 14 Finally 15 CloseDatabase() 16 End Try 17 End If 18 End Sub
CalculationExamples.vb: CountPredicate
01Private Class CountPredicate 02 Inherits Query.Predicate 03 04 Public countTable As Hashtable = New Hashtable() 05 06 Public Function Match(ByVal p As Pilot) As Boolean 07 ' return all Professional and Test pilots and count in 08 ' each category 09 Dim keywords As String() = {"Professional", "Test"} 10 Dim keyword As String 11 For Each keyword In keywords 12 If (p.Name.StartsWith(keyword)) Then 13 If countTable.ContainsKey(keyword) Then 14 countTable(keyword) = CType(countTable(keyword), Integer) + 1 15 Else 16 countTable.Add(keyword, 1) 17 End If 18 Return True 19 End If 20 Next 21 Return False 22 End Function 23 End Class