Reputation: 2108
In a project I'm working on, I have four entities(amongst a bunch of others), WorkOrder, Crew, CrewAssignment, and Contractor. Their relationship is like this:
The problem I'm having is setting up the last part where a WorkOrder can have multiple CrewAssignments. What I want to do is ensure that the WorkOrder.CrewAssignments property only returns the CrewAssignments that have a Crew with the same Contractor as the WorkOrder. Or, less wordy, "where WorkOrder.Contractor == CrewAssignment.Crew.Contractor".
The only thing I've been able to come up with is this, but it throws an exception about the x variable being undefined.
HasMany(x => x.CrewAssignments).KeyColumn("WorkOrderID").Where(x => x.Crew.Contractor == x.WorkOrder.Contractor);
Is doing something like this even possible? Or am I barking up the wrong tree entirely? Google's been failing me all morning with this one. Any ideas?
Upvotes: 1
Views: 2114
Reputation: 16033
I don't know if it could help you but here is my take on in.
In FluentNHibernate you have a method called ApplyFilter
that you can set up in your mappings in order to filter on child collections :
HasMany(x => x.CrewAssignments).KeyColumn("WorkOrderID").ApplyFilter<MyFilter>().Cascade.AllDeleteOrphan();
Then you can create your filter :
public class MyFilter: FilterDefinition
{
public MyFilter()
{
WithName("contractor").WithCondition("Crew.ContractorId== :contractorId").AddParameter("contractorId", NHibernate.NHibernateUtil.Int32);
}
}
Then in the implementation of your repository, you invoke the filter :
Sessions.EnableFilter("contractor").SetParameter("contractorId", 1254);
I don't know if this is the best solution but the only one that comes to my mind right now.
Upvotes: 5