Parameterized NQ

The following examples show how to pass parameters to the Native Query predicate. Store Pilots function is used to fill in the database. 

GetTestPilots

Using predicate constructor to specify the querying parameter. 

ParameterizedExamples.cs: GetTestPilots
01private static void GetTestPilots() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IObjectSet result = container.Query(new PilotNamePredicate("Test")); 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 }
ParameterizedExamples.cs: PilotNamePredicate
01private class PilotNamePredicate : Predicate 02 { 03 private string startsWith; 04 05 public PilotNamePredicate(string startsWith) 06 { 07 this.startsWith = startsWith; 08 } 09 10 public bool Match(Pilot pilot) 11 { 12 return pilot.Name.StartsWith(startsWith); 13 } 14 }

ParameterizedExamples.vb: GetTestPilots
01Private Shared Sub GetTestPilots() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim result As IObjectSet = container.Query(New PilotNamePredicate("Test")) 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
ParameterizedExamples.vb: PilotNamePredicate
01Private Class PilotNamePredicate 02 Inherits Predicate 03 Private startsWith As String 04 05 Public Sub New(ByVal startsWith As String) 06 Me.startsWith = startsWith 07 End Sub 08 09 Public Function Match(ByVal p As Pilot) As Boolean 10 Return p.Name.StartsWith(startsWith) 11 End Function 12 End Class

GetProfessionalPilots

Using a function to process the NQ.

ParameterizedExamples.cs: GetProfessionalPilots
01private static void GetProfessionalPilots() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IList<Pilot> result = ByNameBeginning("Professional"); 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 }
ParameterizedExamples.cs: ByNameBeginning
1private static IList<Pilot> ByNameBeginning(string startsWith) 2 { 3 return Database().Query<Pilot>(delegate(Pilot pilot) 4 { 5 return pilot.Name.StartsWith(startsWith); 6 }); 7 }

 

No analogue in VB.