How do I write a ICriteria that matches this linq?
ProgramItems.OrderBy(x => x.TimeBlocks.Min(y => y.StartTime))
ProgramItem - TimeBlock has a many to many relationship.
I can filter by a TimeBlock id with this code:
criteria.Add(Subqueries.Exists(DetachedCriteria.For<ProgramItem>("p")
.CreateAlias("p.TimeBlocks", "timeBlocks")
.Add(Restrictions.EqProperty("p.Id", "ProgramItem.Id"))
.Add(Restrictions.Eq("timeBlocks.Id", request.TimeBlockId))
.SetProjection(Projections.GroupProperty("p.Id"))
.Add(Restrictions.Eq(Projections.Count("p.Id"), 1))));
But I can't find a way to use the alias/projection to order by a property in TimeBlock - any clue?
The way how to ORDER BY via
many-to-manyend would look like this:As we can see, both ends of
many-to-manymust be mapped. We are then building the MIN part over theTimeBlockentity - with a JOIN to theProgramItemtable. That is used in a root query as a way how to filter.My suggestion would be to change
many-to-manyto have explicit entity for a pairing table. That would lead to much more simplified queries... but in this case we can do it even with this