How to achieve an eagerly loaded, filtered child collection with the NHibernate criteria API
- by vakman
Is it possible to use the criteria api to load a set of parent objects along with a filtered, eagerly loaded set of child objects? I'm trying to query a list of categories and at the same time load the categories products that start with the letter M. The query below gives me the results I want but the Products are not eagerly loaded, that is NHibernate performs additional queries when I enumerate the Product collection:
var categoriesWithProducts = session.CreateCriteria<Category>()
.SetFetchMode("Products", FetchMode.Eager)
.CreateCriteria("Products")
.Add(Expression.Like("Name", "M%"))
.List<Category>();
What am I missing here?