Is there a way to load an NHibernate entity, with a clause on a child collection? I have a scenario where I log changes in “Operations” i.e. one operation can contain changes to multiple entities. When I want to load the log for a specific entity, I load all Operations with any Changes made to that entity. Loading these Operations cause all changes to be loaded – I only want the relevant changes to be loaded.
Classes:
public class Operation{
public virtual DateTime TimeStamp { get; set; }
public virtual IList<Change> Changes { get; private set; }
}
public class Change{
public virtual string ChangeText { get; set; }
public virtual int EntityId { get; set; }
}
Getting the operations for a given entity
Session.QueryOver<Operation>().Where(o => o.Changes.Any(c => c.EntityId == entityId));
I. As stated in this Oskar Berggren’s answer: https://stackoverflow.com/a/13864061/1679310
you can apply filter 18.1. NHibernate filters
Summary:
Adjust your mapping
And then call it this way:
II. Other option is to filter
Changeswhen quering theOperation: 16.4. Associations