Here's a collection of SODA-query examples. These queries assume that there's a Pilot class with a name and age, a Car class with a pilot and name and a BlogPost class with list of tags, authors and a Dictionary of meta-data.
There are also a few examples for special cases.
This is the most basic and most used constraint for SODA-queries. SODA acts like a filter on all stored objects. But usually you're only interested for instances of a certain type. Therefore you need to constrain the type of the result.
IQuery query = container.Query(); query.Constrain(typeof(Pilot)); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) Dim result As IObjectSet = query.Execute()
You can add constrains on fields. This is done by descending into a field and constrain the value of that field. By default the constrain is an equality comparison.
IQuery query = container.Query(); query.Constrain(typeof (Pilot)); query.Descend("name").Constrain("John"); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) query.Descend("name").Constrain("John") Dim result As IObjectSet = query.Execute()
You can do comparison on the field-values. For example to check if something is greater or smaller than something else.
IQuery query = container.Query(); query.Constrain(typeof (Pilot)); query.Descend("age").Constrain(42).Greater(); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) query.Descend("age").Constrain(42).Greater() Dim result As IObjectSet = query.Execute()
IQuery query = container.Query(); query.Constrain(typeof (Pilot)); query.Descend("age").Constrain(42).Greater().Equal(); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) query.Descend("age").Constrain(42).Greater().Equal() Dim result As IObjectSet = query.Execute()
You can combine different constraints with an 'AND' or 'OR' condition. By default all constrains are combined with the 'AND' condition.
IQuery query = container.Query(); query.Constrain(typeof (Pilot)); query.Descend("age").Constrain(42).Greater() .Or(query.Descend("age").Constrain(30).Smaller()); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) query.Descend("age").Constrain(42).Greater().[Or](query.Descend("age").Constrain(30).Smaller()) Dim result As IObjectSet = query.Execute()
You can invert a constrain by calling the not-method.
IQuery query = container.Query(); query.Constrain(typeof (Pilot)); query.Descend("age").Constrain(42).Not(); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) query.Descend("age").Constrain(42).Not() Dim result As IObjectSet = query.Execute()
There are special compare operations for strings. By default strings are compared by equality and the comparison is case sensitive.
There's the contains-comparison which checks if a field contains a substring. The like-comparison is the case-insensitive version of the contains-comparison.
Also a start-with- and a ends-with-caparison is available for strings. For this you can specify if the comparison is case sensitive or not.
IQuery query = container.Query(); query.Constrain(typeof (Pilot)); // First strings, you can use the contains operator query.Descend("name").Constrain("oh").Contains() // Or like, which is like .contains(), but case insensitive .Or(query.Descend("name").Constrain("AnN").Like()) // The .endsWith and .startWith constrains are also there, // the true for case-sensitive, false for case-insensitive .Or(query.Descend("name").Constrain("NY").EndsWith(false)); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Pilot)) ' First strings, you can use the contains operator ' Or like, which is like .contains(), but case insensitive ' The .endsWith and .startWith constrains are also there, ' the true for case-sensitive, false for case-insensitive query.Descend("name").Constrain("oh").Contains().[Or](query.Descend("name").Constrain("AnN").[Like]()).[Or](query.Descend("name").Constrain("NY").EndsWith(False)) Dim result As IObjectSet = query.Execute()
When you have a reference type field, you can compare this field with a certain object. It will compare the field and the object by object identity.
Note that this comparison only works with stored objects. When you use a not yet stored object as constrain, it will use query by example. To force a comparison by object identity, you can add a .Identiy() call.
Pilot pilot = container.Query<Pilot>()[0]; IQuery query = container.Query(); query.Constrain(typeof(Car)); // if the given object is stored, its compared by identity query.Descend("pilot").Constrain(pilot); IObjectSet carsOfPilot = query.Execute();
Dim pilot As Pilot = container.Query(Of Pilot)()(0) Dim query As IQuery = container.Query() query.Constrain(GetType(Car)) ' if the given object is stored, its compared by identity query.Descend("pilot").Constrain(pilot) Dim carsOfPilot As IObjectSet = query.Execute()
You can descend deeper into the objects by following fields. This allows you to setup complex constraints on nested objects. Note that the deeper you descend into the objects, the more expensive the query is to execute.
IQuery query = container.Query(); query.Constrain(typeof (Car)); query.Descend("pilot").Descend("name").Constrain("John"); IObjectSet result = query.Execute();
Dim query As IQuery = container.Query() query.Constrain(GetType(Car)) query.Descend("pilot").Descend("name").Constrain("John") Dim result As IObjectSet = query.Execute()