LINQ Collection

This topic applies to .NET version only

This chapter provides a collection of LINQ examples with db4o, which you can use to find different ways to construct your queries. The information below covers most of the database querying operations and should be enough for the majority of cases. However, it is still advisable to study MSDN resources, which explain in detail how LINQ and Extension methods work. For the introduction to LINQ queries for db4o please refer to LINQ chapter.

Simple Select

Let's fill up the database with some data:

In order to select objects of only Pilot class we can issue the following query:

Note, that where clause is optional: if it is not specified all objects of Pilot class will be returned.

We can make querying even easier by using anonymous types:

Database Object Clone

You can easily use LINQ to create database objects clones:

Retrieved objects are not bound to the object container, but they duplicate the values from the database. You can use them for an "object-readonly" mode.

Select Any Type

With the selection we are not bound to only one type of objects - actually we can select everything that is currently in the database:

We can further use this broad selection too. As the query result implements IQueryable interface, we can re-use it to retrieve more specific objects:

In this example we use an all objects selection to find a range of pilots and then cars with pilots within the range. Remember, however, that the objects in allObjects variable are not actually retrieved from the database until they are browsed or used. If you want to get them all into the memory for future use, utilize ToList or ToArray methods.

Join Query

Above we've already discussed how to select objects of one type based on the selection of the other type. However, that way is rather cumbersome and most probably you won't need it: you can use join operator instead:

This simple syntax allows you to join any amount of classes using any possible relationship between them. You can also make use of Join extension method syntax to get the same result.

Sorting

Sorting is added with orderby operator and can be ascending(default) or descending.

We can also use subsequent sorts. For example sort by name descending and by points ascending:

The same effect can be achieved by using OrderBy/OrderByDescending and ThenBy/ThenByDescending extension methods.

Grouping Results

Grouping results can be often useful in different reports. For example, we can group the data from the previous example by pilot's name:

Here we use GroupBy extension method. The parameters specify the grouping property (Points) and the display property (Name).

Modified Result

Sometimes we need to apply some calculation on each result - with LINQ we can do that directly in the query:

In the example above the query will bring as the calculated percentage of maximum for each pilot's points.

Selecting A Result

Though a query usually returns all the candidates matching the selected criteria, we can specify which of the results we want: First, ElementAt, Any, All.

The query above will return true if there are any objects in the result set and false otherwise.

Aggregate Functions

Often we are not interested in each and every result, but need some statistics about it: sum, aggregate, average, max etc. This can be achieved with extension methods.

This method returns only the average value of pilot points.

In this case we use Aggregate extension method to return all the names and a semicolon-separated string. In Aggregate function first parameter specify the initial return value, second parameter is a function that appends each new value to the initial value.