Simple Example

All that you need to enable LINQ in your project is to add reference to Db4objects.Db4o.Linq.dll and use it in your class:

c#:

// also requires to reference System assembly

using System.Linq;

using Db4objects.Db4o.Linq;

VB:

Imports Db4objects.Db4o.Linq

Let's start from looking at some simple examples. First of all - fill up the database with some objects:

LinqExample.cs: StoreObjects
01private static void StoreObjects() 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer container = Database(); 05 if (container != null) 06 { 07 try 08 { 09 Pilot pilot; 10 Car car; 11 for (int i = 0; i < ObjectCount; i++) 12 { 13 pilot = new Pilot("Test Pilot #" + i, i + 10); 14 car = new Car("Test model #" + i, pilot); 15 container.Store(car); 16 } 17 container.Commit(); 18 } 19 catch (Db4oException ex) 20 { 21 System.Console.WriteLine("Db4o Exception: " + ex.Message); 22 } 23 catch (Exception ex) 24 { 25 System.Console.WriteLine("System Exception: " + ex.Message); 26 } 27 finally 28 { 29 CloseDatabase(); 30 } 31 } 32 }
LinqExample.vb: StoreObjects
01Private Shared Sub StoreObjects() 02 File.Delete(Db4oFileName) 03 Dim container As IObjectContainer = Database() 04 If container IsNot Nothing Then 05 Try 06 Dim pilot As Pilot 07 Dim car As Car 08 For i As Integer = 0 To ObjectCount - 1 09 pilot = New Pilot("Test Pilot #" + i.ToString(), i + 10) 10 car = New Car("Test model #" + i.ToString(), pilot) 11 container.Store(car) 12 Next 13 container.Commit() 14 Catch ex As Db4oException 15 System.Console.WriteLine("Db4o Exception: " + ex.Message) 16 Catch ex As Exception 17 System.Console.WriteLine("System Exception: " + ex.Message) 18 Finally 19 CloseDatabase() 20 End Try 21 End If 22 End Sub

And use LINQ syntax to select Pilots by name and points:

LinqExample.cs: SelectPilotByNameAndPoints
01private static void SelectPilotByNameAndPoints() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) 05 { 06 try 07 { 08 IEnumerable<Pilot> result = from Pilot p in container 09 where p.Name.StartsWith("Test") && p.Points > 2 10 select p; 11 12 ListResult(result); 13 } 14 catch (Exception ex) 15 { 16 System.Console.WriteLine("System Exception: " + ex.Message); 17 } 18 finally 19 { 20 CloseDatabase(); 21 } 22 } 23 }
LinqExample.vb: SelectPilotByNameAndPoints
01Private Shared Sub SelectPilotByNameAndPoints() 02 Dim container As IObjectContainer = Database() 03 If container IsNot Nothing Then 04 Try 05 Dim pilots = From item In container _ 06 Where item.GetType Is GetType(Pilot) Select CType(item, Pilot) 07 Dim result As IEnumerable(Of Pilot) = From p In pilots _ 08 Where p.Name.StartsWith("Test") And p.Points > 2 Select p 09 ListResult(result) 10 Finally 11 CloseDatabase() 12 End Try 13 End If 14 End Sub

We use our db4o database as a datasource, the rest of the syntax is typical for LINQ queries. Using Object-Oriented language for creating queries gives us an advantage of creating very flexible select criteria and provides a benefit of compile-checked code.