OK so I can't find a good example of this so I can better understand how to use detached criteria (assuming that's what I want to use in the first place).
I have 2 tables. Placement and PlacementSupervisor
My PlacementSupervisor table has a FK of PlacementID which relates to Placement.PlacementID - though my nhibernate model class has PlacementSupervisor . Placement (rather than specifically specifying a property of placement ID - not sure if this is important).
What I am trying to do is - if values are passed through for the supervisor ID I want to restrict placements with that supervisor id.
Have tried:
ICriteria query = m_PlacementRepository.QueryAlias("p")
....
if (criteria.SupervisorId > 0 && !string.IsNullOrEmpty(criteria.SupervisorTypeId))
{
DetachedCriteria entityQuery = DetachedCriteria.For<PlacementSupervisor>("sup")
.Add(Restrictions.And(
Restrictions.Eq("sup.supervisorId", criteria.SupervisorId),
Restrictions.Eq("sup.supervisorTypeId", criteria.SupervisorTypeId)
))
.SetProjection(Projections.ProjectionList()
.AddPropertyAlias("Placement.PlacementId", "PlacementId")
);
query.Add(Subqueries.PropertyIn("p.PlacementId", entityQuery));
}
Which just gives me the error: Could not find a matching criteria info provider to: (sup.supervisorId = 5 and sup.supervisorTypeId = U)
Firstly supervisorTypeId is a string. Secondly I don't understand how to achieve what I'm trying to do so have just been trying various combinations of projections, and property aliases and subquery options..as I don't get how I'm supposed to join to another table/entity when the FK key sits in the second table.
Can someone point me in the right direction. It seems like such an easy thing to do from a data perspective that hopefully I'm just missing something obvious!!
I ended up being able to use the code from above modified.
query.CreateCriteria("Supervisors")
.Add(Restrictions.Eq("SupervisorId", (int)criteria.SupervisorId))
.Add(Restrictions.Eq("SupervisorType.SupervisorTypeId", criteria.SupervisorTypeId));
With Supervisors being a property on my Placement model class.
Note also that Expressions has been semi-deprecated by Restrictions.