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:

  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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top