The following examples show how to select objects matching a specific criteria. Store Pilots function is used to fill in the database.
Select only those pilots, which have 5 points.
01private static void SelectPilot5Points() 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
// pilots with 5 points are included in the 11
// result 12
return pilot.Points == 5; 13
}); 14
ListResult(result); 15
} 16
catch (Exception ex) 17
{ 18
System.Console.WriteLine("System Exception: " + ex.Message); 19
} 20
finally 21
{ 22
CloseDatabase(); 23
} 24
} 25
}
01Private Shared Sub SelectPilot5Points() 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 Pilot5PointsMatch) 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 Pilot5PointsMatch(ByVal p As Pilot) As Boolean 2
' pilots with 5 points are included in the 3
' result 4
Return p.Points = 5 5
End Function
Select all pilots, whose name includes "Test".
01private static void SelectTestPilots() 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
// are included in the result 12
return pilot.Name.IndexOf("Test") >= 0; 13
}); 14
ListResult(result); 15
} 16
catch (Exception ex) 17
{ 18
System.Console.WriteLine("System Exception: " + ex.Message); 19
} 20
finally 21
{ 22
CloseDatabase(); 23
} 24
} 25
}
01Private Shared Sub SelectTestPilots() 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 TestPilotsMatch) 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 TestPilotsMatch(ByVal p As Pilot) As Boolean 2
' all Pilots containing "Test" in the name 3
' are included in the result 4
Return p.Name.IndexOf("Test") >= 0 5
End Function
Select those pilots, whose name (number) ends with 6.
01private static void SelectPilotsNumberX6() 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 with the name ending with 6 will 11
// be included 12
return pilot.Name.EndsWith("6"); 13
}); 14
ListResult(result); 15
} 16
catch (Exception ex) 17
{ 18
System.Console.WriteLine("System Exception: " + ex.Message); 19
} 20
finally 21
{ 22
CloseDatabase(); 23
} 24
} 25
}
01Private Shared Sub SelectPilotsNumberX6() 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 PilotsX6Match) 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 PilotsX6Match(ByVal p As Pilot) As Boolean 2
' all Pilots with the name ending with 6 will 3
' be included 4
Return p.Name.EndsWith("6") 5
End Function