Finding parents where child collection does not contain an item
Posted
by Colin Bowern
on Stack Overflow
See other posts from Stack Overflow
or by Colin Bowern
Published on 2010-04-28T21:46:25Z
Indexed on
2010/04/28
21:47 UTC
Read the original article
Hit count: 234
nhibernate
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.
© Stack Overflow or respective owner