Simple Selection

The following examples show how to use NQ to select all objects of the specified type from a database. Store Pilots function is used to fill in the database.

SelectAllPilots

For languages with generics support (Java5-6; .NET2.0-3.0):

SimpleExamples.cs: SelectAllPilots
01private static void SelectAllPilots() 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 // each Pilot is included in the result 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 }

SimpleExamples.vb: SelectAllPilots
01Private Shared Sub SelectAllPilots() 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) 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
SimpleExamples.vb: AllPilotsMatch
1Private Shared Function AllPilotsMatch(ByVal p As Pilot) As Boolean 2 ' each Pilot is included in the result 3 Return True 4 End Function

SelectAllPilotsNonGeneric

For languages without generics support (Java1.1-1.4; .NET1.0):

SimpleExamples.cs: SelectAllPilotsNonGeneric
01private static void SelectAllPilotsNonGeneric() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IObjectSet result = container.Query(new NonGenericPredicate()); 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 }
SimpleExamples.cs: NonGenericPredicate
01private class NonGenericPredicate : Predicate 02 { 03 public bool Match(object obj) 04 { 05 // each Pilot is included in the result 06 if (obj is Pilot) 07 { 08 return true; 09 } 10 return false; 11 } 12 }

SimpleExamples.vb: SelectAllPilotsNonGeneric
01Private Shared Sub SelectAllPilotsNonGeneric() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim result As IObjectSet = container.Query(New NonGenericPredicate()) 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
SimpleExamples.vb: NonGenericPredicate
01Private Class NonGenericPredicate 02 Inherits Predicate 03 Public Function Match(ByVal obj As Object) As Boolean 04 ' each Pilot is included in the result 05 If TypeOf obj Is Pilot Then 06 Return True 07 End If 08 Return False 09 End Function 10 End Class