Starting from NHibernate 3.0 a new API was build as a wrapper around the ICriteria object.
Obviously I’m talking about the powerful API called QueryOver. With this API you can build a wide range of query object, from the simplest to the more complicated one. But you need to remember that this API is not a Linq provider, and then you cannot apply those constructs typically applied in a Linq query. I explain with an example what I really mean.
Introduction: you have a data model with 2 table, Category and Customer, with a one-to-many relationship between them.
With Entity Framework and Linq To Entities it’s pretty simple to query data that belong to both entities, thanks to navigation properties and Linq provider, i.e. all customers that belong to a category with a specified Id, as shown in this example:
var customerList = customers.Where(c => c.CategoryId == 1);
If you try to execute the same query with the same where conditions applied to a QueryOverObject, like this example:
QueryOver<Customer>; qu = QueryOver.Of<Customer>() .Where(c => c.CategoryId == 1);
This code throws a NHibernateException saying that “could not resolve property: Category.Id of : MyNamespace.Customer”, suggesting to verify the mapping as property “Category.Id” could not be found”.
Obviously such a property doesn’t exist at all, it’s only the QueryOver API that concatenates the Navigation property and and its field named Id (in this example).
This means that: you cannot make a query with QueryOver API that refers to fields belonging to a navigation property in where clause….without explicitly defying a join between the object itself and its navigation property. An example will help to understand better.
Category cat = null; QueryOver<Customer> query = QueryOver.Of<Customer>() .JoinAlias(x => x.Category, () =>; cat) .Where(x => cat.Id == 1);
I have defined a join with the JoinAlias method, which uses an alias (the Category object declared two rows before) to project the object under the navigation property. After that you can use this alias inside the others method (such as “Where” in this example) to refer to the navigation property field (cat.Id).
As you can see, the way you write the lambda expression inside a Linq provider’s where clause is quite different than the “”Where” condition of a QueryObject object.
Not even the logical operator “And” can be used in the same way. To apply this logical operator to the “where” filter you have to use the “And” method, as shown here:
Category cat = null; QueryOver<Customer> query = QueryOver.Of<Customer>() .JoinAlias(x => x.Category, () => cat) .Where(x => cat.Id == 1) .And(x => cat.IsEnabled);