You are here: Basics Operations & Concepts > Querying > Native Queries > Equality

Native Query Examples

Here's a collection of native query examples. These queries assume that there's a Pilot class with a name and age and a Car class with a pilot and name.

Note that for .NET 3.5 or newer we recommend to use LINQ instead of native queries.

Equality

This query shows you how compare a property for equality. In this example we compare the name of a person.

IList<Pilot> result = container.Query(
    delegate(Pilot pilot) { return pilot.Name == "John"; });
NativeQueryExamples.cs: Check for equality of the name
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryJohns)
NativeQueryExamples.vb: Check for equality of the name
Private Shared Function QueryJohns(ByVal pilot As Pilot)
    Return pilot.Name = "John"
End Function
NativeQueryExamples.vb: Query for John

Comparison

You can compare values with the usual comparison operators.

IList<Pilot> result = container.Query(
    delegate(Pilot pilot) { return pilot.Age < 18; });
NativeQueryExamples.cs: Compare values to each other
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryAdults)
NativeQueryExamples.vb: Compare values to each other
Private Shared Function QueryAdults(ByVal pilot As Pilot)
    Return pilot.Age < 18
End Function
NativeQueryExamples.vb: Query for adults

Query For Value Range

Of course you can combine different comparisons. For example you can combine the greater and smaller than operators to check for a range of values.

IList<Pilot> result = container.Query(
    delegate(Pilot pilot) { return pilot.Age > 18 && pilot.Age < 30; });
NativeQueryExamples.cs: Query for a particular rage of values
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryRange)
NativeQueryExamples.vb: Query for a particular rage of values
Private Shared Function QueryRange(ByVal pilot As Pilot)
    Return pilot.Age > 18 AndAlso pilot.Age < 30
End Function
NativeQueryExamples.vb: Query for range

Combine Check With Logical Operators

Of course you can combine a arbitrary set of conditions with logical operators.

IList<Pilot> result = container.Query(
    delegate(Pilot pilot)
        {
            return (pilot.Age > 18 && pilot.Age < 30)
                   || pilot.Name == "John";
        });
NativeQueryExamples.cs: Combine different comparisons with the logical operators
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf CombineCriterias)
NativeQueryExamples.vb: Combine different comparisons with the logical operators
Private Shared Function CombineCriterias(ByVal pilot As Pilot)
    Return (pilot.Age > 18 AndAlso pilot.Age < 30) OrElse pilot.Name = "John"
End Function
NativeQueryExamples.vb: Combine criterias

Query In Separate Method

You can implement your query in a separate method and then just us it where you need it. This is especially useful when you reuse the same query multiple times. Or you want to give your query a clear name for documentation purposes.

First write your method:

private static bool AllJohns(Pilot pilot)
{
    return pilot.Name == "John";
}
NativeQueryExamples.cs: Query as method
Private Shared Function AllJohns(ByVal pilot As Pilot) As Boolean
    Return pilot.Name = "John"
End Function
NativeQueryExamples.vb: Query as method

And then use it:

IList<Pilot> result = container.Query(new Predicate<Pilot>(AllJohns));
NativeQueryExamples.cs: Use the predefined query
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf AllJohns)
NativeQueryExamples.vb: Use the predefined query

Arbitrary Code

In principal your query can contain any code and can do the most complex comparisons. However in practice the are limitations. The simple queries are optimized and translated to SODA-queries. This is not possible for complex queries. If the query cannot be optimized, db4o will instantiate all objects and pass it to your query-object. This is a order of magnitude slower than a optimized native query and only feasible for smaller data sets.

IList<int> allowedAges = Array.AsReadOnly(new int[] {18, 20, 35});
IList<Pilot> result = container.Query(
    delegate(Pilot pilot)
        {
            return allowedAges.Contains(pilot.Age) ||
                   pilot.Name.ToLowerInvariant() == "John";
        });
NativeQueryExamples.cs: Arbitrary code
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryWithAnyCode)
NativeQueryExamples.vb: Arbitrary code
Private Shared Function QueryWithAnyCode(ByVal pilot As Pilot)
    Dim allowedAges As IList(Of Integer) = Array.AsReadOnly(New Integer() {18, 20, 35})
    Return allowedAges.Contains(Pilot.Age) _
                OrElse Pilot.Name.ToLowerInvariant() = "John"
End Function
NativeQueryExamples.vb: Query with arbitrary code