Combined Result Sets

This set of examples shows how to use NQ to select objects from more than one result sets. Store Cars and Store Persons functions are used to fill in the database. 

SelectPilotsAndTrainees

Selects all pilots and trainees using Person superclass. 

MultiExamples.cs: SelectPilotsAndTrainees
01private static void SelectPilotsAndTrainees() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IList<Person> result = container.Query<Person>(delegate(Person person) 09 { 10 // all persons 11 return true; 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 }
MultiExamples.vb: SelectPilotsAndTrainees
01Private Shared Sub SelectPilotsAndTrainees() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim result As IList(Of Person) = container.Query(Of Person)(AddressOf AllPersonMatch) 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
MultiExamples.vb: AllPersonMatch
1Private Shared Function AllPersonMatch(Of Person)(ByVal p As Person) As Boolean 2 ' all persons 3 Return True 4 End Function

SelectPilotsInRange

Selects all cars that have pilot field in the preselected Pilot array. 

MultiExamples.cs: SelectPilotsInRange
01private static void SelectPilotsInRange() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IObjectSet result = container.Query(new CarPilotPredicate()); 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 }
MultiExamples.cs: CarPilotPredicate
01private class CarPilotPredicate : Predicate 02 { 03 private IList<Pilot> pilots = null; 04 05 private IList<Pilot> GetPilotsList() 06 { 07 if (pilots == null) 08 { 09 pilots = Database().Query<Pilot>( 10 delegate(Pilot pilot) 11 { 12 return pilot.Name.StartsWith("Test"); 13 }); 14 } 15 return pilots; 16 } 17 18 public bool Match(Car car) 19 { 20 // all Cars that have pilot field in the 21 // Pilots array 22 return GetPilotsList().Contains(car.Pilot); 23 } 24 }
MultiExamples.vb: SelectPilotsInRange
01Private Shared Sub SelectPilotsInRange() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim result As IObjectSet = container.Query(New CarPilotPredicate()) 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
MultiExamples.vb: CarPilotPredicate
01Private Class CarPilotPredicate 02 Inherits Query.Predicate 03 04 Private pilots As IList(Of Pilot) = Nothing 05 06 Private Shared Function TestPilotsMatch(ByVal p As Pilot) As Boolean 07 Return p.Name.StartsWith("Test") 08 End Function 09 ' end TestPilotsMatch 10 11 Private Function GetPilotsList() As IList(Of Pilot) 12 If pilots Is Nothing Then 13 pilots = Database().Query(Of Pilot)(AddressOf TestPilotsMatch) 14 End If 15 Return pilots 16 End Function 17 18 Public Function Match(ByVal car As Car) As Boolean 19 20 ' all Cars that have pilot field in the 21 ' Pilots array 22 Return GetPilotsList().Contains(car.Pilot) 23 End Function 24 End Class
MultiExamples.vb: TestPilotsMatch
1Private Shared Function TestPilotsMatch(ByVal p As Pilot) As Boolean 2 Return p.Name.StartsWith("Test") 3 End Function