.NET Syntax

This topic applies to .NET version only.

In .NET version of db4o we need to distinguish NQ syntax for c# (.NET1 and .NET2) and Visual Basic.

 

c# Syntax (.NET1)

  1. This examples shows how to use NQ to select all the objects of the specified type:
    NQSyntaxExamples1.cs: QuerySyntax1
    01private static void QuerySyntax1() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IObjectSet result = container.Query(typeof(Pilot)); 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 }
  2. The following example represents the most generic use of NQ in .NET1. You can modify the code to match any selection criteria:
    NQSyntaxExamples1.cs: QuerySyntax2
    01private static void QuerySyntax2() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IObjectSet result = container.Query(new PilotPredicate()); 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 }
    NQSyntaxExamples1.cs: PilotPredicate
    01private class PilotPredicate: Predicate 02 { 03 public bool Match(object obj) 04 { 05 if (obj is Pilot) 06 { 07 if (((Pilot)obj).Name.StartsWith("Test")) 08 { 09 return true; 10 } 11 } 12 return false; 13 } 14 }
  3. This example can be used when the query results should be sorted:
    NQSyntaxExamples1.cs: QuerySyntax3
    01private static void QuerySyntax3() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IObjectSet result = container.Query(new PilotPredicate(),new PilotComparer()); 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 }
    NQSyntaxExamples1.cs: PilotPredicate
    01private class PilotPredicate: Predicate 02 { 03 public bool Match(object obj) 04 { 05 if (obj is Pilot) 06 { 07 if (((Pilot)obj).Name.StartsWith("Test")) 08 { 09 return true; 10 } 11 } 12 return false; 13 } 14 }
    NQSyntaxExamples1.cs: PilotComparer
    1private class PilotComparer : IComparer 2 { 3 public int Compare(Object pilot1, Object pilot2) 4 { 5 return ((Pilot)pilot1).Points - ((Pilot)pilot2).Points; 6 } 7 }

c# Syntax (.NET2)

  1. This example shows how to use NQ to select all the objects of the specified type:
    NQSyntaxExamples.cs: QuerySyntax1
    01private static void QuerySyntax1() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IList<Pilot> result = container.Query<Pilot>(typeof(Pilot)); 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 }
  2. The same functionality as in the previous example:
    NQSyntaxExamples.cs: QuerySyntax2
    01private static void QuerySyntax2() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IList<Pilot> result = container.Query<Pilot>(); 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 }
  3. Selecting all the objects of the specified type in a sorted order:
    NQSyntaxExamples.cs: QuerySyntax3
    01private static void QuerySyntax3() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IList<Pilot> result = container.Query<Pilot>(new PilotComparer()); 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 }
    NQSyntaxExamples.cs: PilotComparer
    1private class PilotComparer : IComparer<Pilot> 2 { 3 public int Compare(Pilot pilot1, Pilot pilot2) 4 { 5 return pilot1.Points - pilot2.Points; 6 } 7 }
  4. General selection - the selection criteria should be specified in the delegate function:
    NQSyntaxExamples.cs: QuerySyntax4
    01private static void QuerySyntax4() 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 }
  5. Both selection and sorting is used. Delegate functions are used to define the criterias:
    NQSyntaxExamples.cs: QuerySyntax5
    01private static void QuerySyntax5() 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 pilot.Name.StartsWith("Test"); 11 }, 12 delegate(Pilot pilot1, Pilot pilot2) 13 { 14 return pilot1.Points - pilot2.Points; 15 }); 16 ListResult(result); 17 } 18 catch (Exception ex) 19 { 20 System.Console.WriteLine("System Exception: " + ex.Message); 21 } 22 finally 23 { 24 CloseDatabase(); 25 } 26 } 27 }
  6. Similar to the previous example, but the comparator functionality is moved to a separate class:
    NQSyntaxExamples.cs: QuerySyntax6
    01private static void QuerySyntax6() 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 pilot.Name.StartsWith("Test"); 11 }, 12 new PilotComparer()); 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 }
    NQSyntaxExamples1.cs: PilotComparer
    1private class PilotComparer : IComparer 2 { 3 public int Compare(Object pilot1, Object pilot2) 4 { 5 return ((Pilot)pilot1).Points - ((Pilot)pilot2).Points; 6 } 7 }
  7. The following example uses predicate and comparator in separate classes, which can be helpful for parameterized queriescreate it:
    NQSyntaxExamples.cs: QuerySyntax7
    01private static void QuerySyntax7() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IObjectSet result = container.Query(new PilotPredicate(), new PilotPointsComparer()); 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 }
    NQSyntaxExamples.cs: PilotPointsComparer
    1private class PilotPointsComparer : IComparer 2 { 3 public int Compare(object pilot1, object pilot2) 4 { 5 return ((Pilot)pilot1).Points - ((Pilot)pilot2).Points; 6 } 7 }
    NQSyntaxExamples.cs: PilotPredicate
    01private class PilotPredicate : Db4objects.Db4o.Query.Predicate 02 { 03 public bool Match(object obj) 04 { 05 if (obj is Pilot) 06 { 07 return true; 08 } 09 return false; 10 } 11 }

 

VB.NET Syntax

  1. This example shows how to select all the objects of the specified type. Can be used with and without generics:
    NQSyntaxExamples.vb: QuerySyntax1
    01Private Shared Sub QuerySyntax1() 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)(GetType(Pilot)) 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
  2. This is the most commonly used syntax, which allows to specify any criteria in the function passed to the query:
    NQSyntaxExamples.vb: QuerySyntax2
    01Private Shared Sub QuerySyntax2() 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 PilotTestMatch) 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
    NQSyntaxExamples.vb: PilotTestMatch
    1Private Shared Function PilotTestMatch(ByVal p As Pilot) As Boolean 2 ' test pilots 3 Return p.Name.StartsWith("Test") 4 End Function
  3. Similar to the previous example, but without generics. Separate class is used to specify the query criteria, which can be useful when a parameter should be passed to the query condition. For more information see Parameterized_NQcreate it.
    NQSyntaxExamples.vb: QuerySyntax3
    01Private Shared Sub QuerySyntax3() 02 Dim container As IObjectContainer = Database() 03 If Not container Is Nothing Then 04 Try 05 Dim result As IObjectSet = container.Query(New PilotPredicate()) 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
    NQSyntaxExamples.vb: PilotPredicate
    01Private Class PilotPredicate 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
  4. NQ example with sorted results:
    NQSyntaxExamples.vb: QuerySyntax4
    01Private Shared Sub QuerySyntax4() 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 PilotTestMatch, AddressOf ComparePilots) 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
    NQSyntaxExamples.vb: ComparePilots
    1Private Shared Function ComparePilots(ByVal pilot1 As Pilot, ByVal pilot2 As Pilot) As Integer 2 Return pilot1.Points - pilot2.Points 3 End Function
    NQSyntaxExamples.vb: PilotTestMatch
    1Private Shared Function PilotTestMatch(ByVal p As Pilot) As Boolean 2 ' test pilots 3 Return p.Name.StartsWith("Test") 4 End Function