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.
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
}
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
}
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
}
In .NET1 NQ predicate must be expressed in a separate class ( NQ delegate
methods are only available since .NET2).
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
}
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
}
1private class PilotComparer : IComparer 2
{ 3
public int Compare(Object pilot1, Object pilot2) 4
{ 5
return ((Pilot)pilot1).Points - ((Pilot)pilot2).Points; 6
} 7
}
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
}
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
}
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
}
1private class PilotComparer : IComparer<Pilot> 2
{ 3
public int Compare(Pilot pilot1, Pilot pilot2) 4
{ 5
return pilot1.Points - pilot2.Points; 6
} 7
}
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
}
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
}
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
}
1private class PilotComparer : IComparer 2
{ 3
public int Compare(Object pilot1, Object pilot2) 4
{ 5
return ((Pilot)pilot1).Points - ((Pilot)pilot2).Points; 6
} 7
}
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
}
1private class PilotPointsComparer : IComparer 2
{ 3
public int Compare(object pilot1, object pilot2) 4
{ 5
return ((Pilot)pilot1).Points - ((Pilot)pilot2).Points; 6
} 7
}
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
}
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
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
1Private Shared Function PilotTestMatch(ByVal p As Pilot) As Boolean 2
' test pilots 3
Return p.Name.StartsWith("Test") 4
End Function
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
01Private Class PilotPredicate 02
Inherits Query.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
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
1Private Shared Function ComparePilots(ByVal pilot1 As Pilot, ByVal pilot2 As Pilot) As Integer 2
Return pilot1.Points - pilot2.Points 3
End Function
1Private Shared Function PilotTestMatch(ByVal p As Pilot) As Boolean 2
' test pilots 3
Return p.Name.StartsWith("Test") 4
End Function