You are here: Basics Operations & Concepts > Querying > LINQ

LINQ

Of course, db4o supports .NET LINQ to query the database.

First, you need to add the Db4objects.Db4o.Linq.dll-assembly to your project. This assembly contains db4o LINQ-Implementation. Make also sure that Mono.Reflection.dll is aside it. See "Dependency Overview"

Note that LINQ requires .NET 3.5 or newer!

LINQ Queries

Writing a query is simple. You write the LINQ query against the object-container. First you need to import the LINQ-namespaces.

using System.Linq;
using Db4objects.Db4o.Linq;
LinqExamples.cs: Use the LINQ namespace
Imports System.Linq
Imports Db4objects.Db4o.Linq
LinqExamples.vb: Use the LINQ namespace

After that, you can start to write a LINQ-Query against the object container.

var allPersons = from Person p in container
                 select p;
LinqExamples.cs: Simple query
Dim allPersons = From p In container _
 Select p
LinqExamples.vb: Simple query

In order to learn LINQ, take a look a the LINQ-resources and tutorial out on the Internet. For example the LINQ-Overview at MSDNor at this example-collection.

There are also a few simple LINQ query examples for db4o here.

IQueryable

db4o also support the .NET IQueryable interface. This is useful when you want to build an abstraction layer for db4o or integrate db4o with other frameworks.

IQueryable<Person> personQuerable = container.AsQueryable<Person>();
var adults = from p in personQuerable
             where p.Age > 18
             orderby p.Name
             select p;
LinqExamples.cs: Get a IQueryable-instance
Dim personQuerable As IQueryable(Of Person) = container.AsQueryable(Of Person)()
Dim adults = From p In personQuerable _
 Where p.Age > 18 _
 Order By p.Name _
 Select p
LinqExamples.vb: Get a IQueryable-instance

More on db4o's LINQ Provider

db4o LINQ provider translates the LINQ queries into db4o's low level query API. Read what this query optimization need and what its limits are. See "LINQ Optimization"

Also read how LINQ is supported on the .NET Compact Framework. See "LINQ For Compact Framework"