Requirement: I'm trying to query a list of customers. Each customer can contain a list of contacts. Only active contacts should be returned.
Code:
Session.QueryOver<Customer>()
.Fetch(x => x.Contacts.Where(c => c.Active))
.Eager
.TransformUsing(new DistinctRootEntityResultTransformer())
.Future()
.AsQueryable();
Error: Unrecognised method call in expression x.Contacts.Where(c => c.Active)
So, how can I filter only active contacts?
You don't because doing so would create a mismatch between the database and the domain model. If you were able to do this, the inactive contacts would be deleted when the session is flushed. There are two good options:
IEnumerable<Contact>
that returns active contacts. This allows you to easily filter any set of Contacts to display only the active ones.NHibernate filters may also be an option but I have no experience with them. I favor using extension methods.