For a query you pass in an example object to db4o. db4o will exanimate the object with reflection. Each field which doesn't have the default value will be use as a constrain. This means a number-field which isn't zero, a reference which isn't null or a string which isn't null. In this example we set the name to John. Then db4o will return all pilots with the name John.
Pilot theExample = new Pilot(); theExample.Name = "John"; IList result = container.QueryByExample(theExample);
Dim theExample As New Pilot() theExample.Name = "John" Dim result As IList = container.QueryByExample(theExample)
Or we set the age to 33 and db4o will return all 33 years old pilots.
Pilot theExample = new Pilot(); theExample.Age = 33; IList result = container.QueryByExample(theExample);
Dim theExample As New Pilot() theExample.Age = 33 Dim result As IList = container.QueryByExample(theExample)
When you set multiple values all will be used as constrain. For example when we set the name to Jo and the age to 29 db4o will return all pilots which are 29 years with the name Jo.
Pilot theExample = new Pilot(); theExample.Name = "Jo"; theExample.Age = 29; IList result = container.QueryByExample(theExample);
Dim theExample As New Pilot() theExample.Name = "Jo" theExample.Age = 29 Dim result As IList = container.QueryByExample(theExample)
If you pass a empty example db4o will return all objects of that type.
Pilot example = new Pilot(); IList result = container.QueryByExample(example);
Dim example As New Pilot() Dim result As IList = container.QueryByExample(example)
Alternatively you also can directly pass in the type.
IList result = container.QueryByExample(typeof (Pilot));
Dim result As IList = container.QueryByExample(GetType(Pilot))
When you pass null all objects stored in the database will be returned.
IList result = container.QueryByExample(null);
Dim result As IList = container.QueryByExample(Nothing)
You can also use nested objects as an example. For example with a car and a pilot. We can query for a car which has a pilot with certain constrains. In this example we get the cars which pilot is called Jenny.
Pilot pilotExample = new Pilot(); pilotExample.Name = "Jenny"; Car carExample = new Car(); carExample.Pilot = pilotExample; IList result = container.QueryByExample(carExample);
Dim pilotExample As New Pilot() pilotExample.Name = "Jenny" Dim carExample As New Car() carExample.Pilot = pilotExample Dim result As IList = container.QueryByExample(carExample)
Collections and arrays act a little different. Query by example returns all object which have at least the items in the array or collection from the example. For example it returns the blog post which has the "db4o"-tag in its tag-collection.
BlogPost pilotExample = new BlogPost(); pilotExample.AddTags("db4o"); IList result = container.QueryByExample(pilotExample);
Dim pilotExample As New BlogPost() pilotExample.AddTags("db4o") Dim result As IList = container.QueryByExample(pilotExample)
You can even check that a item in a collection fulfills certain criteria's. For example we can check that the blog post has an author with the name John in its author-collection.
BlogPost pilotExample = new BlogPost(); pilotExample.AddAuthors(new Author("John")); IList result = container.QueryByExample(pilotExample);
Dim pilotExample As New BlogPost() pilotExample.AddAuthors(New Author("John")) Dim result As IList = container.QueryByExample(pilotExample)