Finding parents where child collection does not contain an item with a specific property value
- by Colin Bowern
I am trying to get a list of parents where the child collection does not contain an item of a specific type. The LINQ equivalent would be something like:
dataset.Where(x => x.Items.FirstOrDefault(y => y.Type.Code == "ABC") == null)
The object model is Parent Child (Items) Type Code
If Parent is my aggregate root, how would I model this in NHibernate criteria/query? Here's my first attempt:
var results = session.CreateCriteria<Parent>()
.CreateCriteria("Items")
.CreateCriteria("Type")
.Add(Restrictions.Not(Restrictions.Eq("Code", "ABC")))
.SetResultTransformer(Transformers.DistinctRootEntity)
.List<Parent>();
This doesn't seem to return the right entities - it just returns them all.