have a class structure like so
Permit -> Financial ->ICollection instrumentList
Instrument -> Agency
That is, an instance of Permit contains an instance of Financial
An instance of Financial has a collection of Instrument objects
An instance of Instrument has an Agency
Using NHibernate Critieria, I want to get a list of Permits with Instruments of a certain type of Agency
This code, gets all Permits with Financial info (it may be the case the a Permit does not have Financial info, in which case I don’t need it)
// get all all Permits with Financial info
var financialCriteria = DetachedCriteria.For<Financial>()
.SetProjection(Projections.Property("Permit.Id")); // Permit.Id in Select
queryCriteria.Add(Subqueries.PropertyIn("Id", financialCriteria)); // Permit.Id in Select
Then I want to restrict that list to Agency of a types 2 & 3:
// then restrict to certain Agency types
var instrumentCriteria = DetachedCriteria.For<Instrument>()
.SetProjection(Projections.Property("Id")) // Instrument.Id in Select
.Add(Restrictions.In("Agency", new object[] { 2, 3})); // Where
queryCriteria.Add(Subqueries.PropertyIn("Id", instrumentCriteria)); //
However, when running with the 2nd chunk of code, I get back an empty list when I know there are 2s and 3s.
What I am doing wrong?
If I do understand your scenario correclty, then the
instrumentCriteriareturns the list of Instrument ID. So with this list we should not limit thequeryCriteriabut thefinancialCriteria. So try this:Instead of queryCriteria.Add(Subqueries.PropertyIn(“Id”, instrumentCriteria));
And the result (projection) of the
instrumentCriteriashould be the owningFinancialID:EDIT:
In case, that
Instrumentcould referenceFinancial, or at least has propertyFinancialId, the syntax returning Permit, filtered by Agency will look like this: