You are here: Basics Operations & Concepts > Querying > SODA Query > Contains on Collections and Arrays

SODA Special Cases Examples

This topic contains a examples which demonstrate special behavior for some types in SODA. Take also a look at the other SODA examples.

Contains on Collections and Arrays

Collections and arrays have a special behavior in SODA to make them easier to query. For example you can simple use a constrain directly on a collection-field to check if it contains that value.

Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.

IQuery query = container.Query();
query.Constrain(typeof (BlogPost));
query.Descend("tags").Constrain("db4o");

IObjectSet result = query.Execute();
SodaQueryExamples.cs: Collection contains constrain
Dim query As IQuery = container.Query()
query.Constrain(GetType(BlogPost))
query.Descend("tags").Constrain("db4o")

Dim result As IObjectSet = query.Execute()
SodaQueryExamples.vb: Collection contains constrain

Constrains on Collection Members

When you have a collection or array field, you can simply descend further to the collection-member fields. This allows you query for a object, which has a collection and certain objects in that collection.

Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.

IQuery query = container.Query();
query.Constrain(typeof(BlogPost));
query.Descend("authors").Descend("name").Constrain("Jenny");

IObjectSet result = query.Execute();
SodaQueryExamples.cs: Descend into collection members
Dim query As IQuery = container.Query()
query.Constrain(GetType(BlogPost))
query.Descend("authors").Descend("name").Constrain("Jenny")

Dim result As IObjectSet = query.Execute()
SodaQueryExamples.vb: Descend into collection members

Contains Key on Dictionarys

You can check a dictionary if it contains a certain key. Similar to collections, you just can directly use a constrain on the collection field. This will compare the value with the keys of the Dictionary.

Note that currently collections cannot be indexed and therefore such a constrain can be slow on a large data set.

IQuery query = container.Query();
query.Constrain(typeof (BlogPost));
query.Descend("metaData").Constrain("source");

IObjectSet result = query.Execute();
SodaQueryExamples.cs: Dictionary contains a key constrain
Dim query As IQuery = container.Query()
query.Constrain(GetType(BlogPost))
query.Descend("metaData").Constrain("source")

Dim result As IObjectSet = query.Execute()
SodaQueryExamples.vb: Dictionary contains a key constrain

Return the Objects of a Field

With SODA you can navigate to a field and return the objects of that field. Note that this only works for reference objects and not for value objects like strings and numbers.

IQuery query = container.Query();
query.Constrain(typeof(Car));
query.Descend("name").Constrain("Mercedes");

// returns the pilot of these cars
IObjectSet result = query.Descend("pilot").Execute();
SodaQueryExamples.cs: Return the object of a field
Dim query As IQuery = container.Query()
query.Constrain(GetType(Car))
query.Descend("name").Constrain("Mercedes")

' returns the pilot of these cars
Dim result As IObjectSet = query.Descend("pilot").Execute()
SodaQueryExamples.vb: Return the object of a field

Mixing With Query By Example

When you have a reference type field, you can also use a query by example constrain for that field. Pass a new object as an example for this.

Note that when you pass a persisted object, it will compare it by object identity and not use it as example. You can force this behavior by adding an explicit by example constrain.

IQuery query = container.Query();
query.Constrain(typeof(Car));
// if the given object is not stored,
// it will behave like query by example for the given object
Pilot examplePilot = new Pilot(null, 42);
query.Descend("pilot").Constrain(examplePilot);

IObjectSet carsOfPilot = query.Execute();
SodaQueryExamples.cs: Mix with query by example
Dim query As IQuery = container.Query()
query.Constrain(GetType(Car))
' if the given object is not stored,
' it will behave like query by example for the given object
Dim examplePilot As New Pilot(Nothing, 42)
query.Descend("pilot").Constrain(examplePilot)

Dim carsOfPilot As IObjectSet = query.Execute()
SodaQueryExamples.vb: Mix with query by example

Dynamically Typed

SODA is a dynamically query language. By default SODA acts like a filter on all stored objects. You just add constrains which filters the objects to the desired output.

An example for this behavior: You just add an field-constraint without any type-constrain on the object. This will return all objects which have such a field and match the constrain.

IQuery query = container.Query();
// You can simple filter objects which have a certain field
query.Descend("name").Constrain(null).Not();

IObjectSet result = query.Execute();
SodaQueryExamples.cs: Pure field constrains
Dim query As IQuery = container.Query()
' You can simple filter objects which have a certain field
query.Descend("name").Constrain(Nothing).[Not]()

Dim result As IObjectSet = query.Execute()
SodaQueryExamples.vb: Pure field constrains

This also means that you can query for not existing fields. SODA will not complain if a field doesn't exist. Instead it won't return any object, because no object could satisfy the constrain.

IQuery query = container.Query();
query.Constrain(typeof (Pilot));
// using not existing fields doesn't throw an exception
// but rather exclude all object which don't use this field
query.Descend("notExisting").Constrain(null).Not();

IObjectSet result = query.Execute();
SodaQueryExamples.cs: Using not existing fields excludes objects
Dim query As IQuery = container.Query()
query.Constrain(GetType(Pilot))
' using not existing fields doesn't throw an exception
' but rather exclude all object which don't use this field
query.Descend("notExisting").Constrain(Nothing).[Not]()

Dim result As IObjectSet = query.Execute()
SodaQueryExamples.vb: Using not existing fields excludes objects