How can I sort (n) sleep mode in relation to a property of a child?

I have an object from my domain model that has a child. How can I use a criteria query for an order based on a property of a child?

For instance:

class FooType { public int Id { get; set; } public string Name { get; set; } public BarType Bar { get; set; } } class BarType { public int Id { get; set; } public string Color { get; set; } } ... // WORKS GREAT var orderedByName = _session.CreateCriteria<FooType>().AddOrder(Order.Asc("Name")).List(); // THROWS "could not resolve property: Bar.Color of: FooType" var orderedByColor = _session.CreateCriteria<FooType>().AddOrder(Order.Asc("Bar.Color")).List(); 

What do I need to do to enable this scenario? I am using NHibernate 2.1. Thanks!

+4
source share
1 answer

You need to either add an alias or create a nested criterion for your child. Not sure how to do this in NHibernate, in Hibernate this is done using the createCriteria() and createAlias() methods. Then you should use the alias as a prefix in order.

Refresh Example hibernation code:

 Criteria criteria = session.createCriteria(FooType.class); criteria.createAlias("bar", "b"); criteria.addOrder(Order.asc("b.color")); 

I assume that in NHibernate this would be very similar, although with property / entity names uppercased. Here is an example from NHibernate documentation.

+13
source

All Articles