Question

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?

Was it helpful?

Solution

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:

  1. Create an extension method on IEnumerable<Contact> that returns active contacts. This allows you to easily filter any set of Contacts to display only the active ones.
  2. Create a view model containing only the data you want to display.

NHibernate filters may also be an option but I have no experience with them. I favor using extension methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top