Queries with NDOql and Linq

NDOql is the query language of NDO. You might ask why it is necessary to use a specific query language. The good news is, that NDOql is not a new language, but an adaption of the Sql WHERE clauses to the needs of object based queries. There is not much new to learn if you know a little bit of Sql. 

Most of the NDOql expressions can also be used as Linq queries. In that case you can make full use of IntelliSense. That means your query code is directly checked while you type it. No more typos in query expressions!

To submit a query, an object of the Query class needs to be created:

NDOQuery<Employee> q = new NDOQuery(pm, "lastName LIKE 'Doe'");
var result = q.Execute();
foreach (var e in result)
    ...;

To get a single object, use q.ExecuteSingle();

It is recommended to use query parameters to transfer literals:

NDOQuery<Employee> q = new NDOQuery(pm, "lastName LIKE {0}");  // Like String.Format
q.Parameters.Add("Doe");
var result = q.Execute();

The first parameter of the NDOQuery constructor expects the PersistenceManager object. The second is the filter string that is an equivalent to the WHERE-clauses in SQL.

Linq

The same query in Linq looks like that:

var result = from e in pm.Objects<Employee>() where e.LastName.Like("Doe") select e;

Note: pm.Objects<T>() returns a  virtual table. No query occurs up to this point. The query occurs, if the Select() method of the virtual table is called.

Second Note: The where clause in the Linq expression will be converted to a query with a parameter. So the Linq query is equivalent to the following code:

NDOQuery<Employee> q = new NDOQuery(pm, "lastName LIKE {0}");
q.Parameters.Add("Doe");
var result = q.Execute();