2
votes

I'm struggling with using QueryOver.OrderBy with strings for property names on child entities. e.g. the following works but I am hardcoding the OrderBy field.

Customer custAlias = null;
session.QueryOver<Campaign>()
       .JoinAlias(x => x.Customer, () => custAlias)
       .OrderBy(() => custAlias.Name).Desc()   // want to use string property name
       .List();

I can specify the OrderBy using a string with something like:

       .OrderBy(Projections.Property("DOB")).Desc();

But this is looking for "DOB" on the Campaign entity, not the child Customer entity. Is it possible to retrieve the alias used by NH and then set the path to the property e.g.

       .OrderBy(Projections.Property("cust.DOB")).Desc(); // where "cust" is the alias

Any ideas?

1

1 Answers

2
votes

The alias used is the name of the variable. So

Projections.Property("custAlias.DOB")

(can't test now, but if I remember corretly it works)

Interestingly, it isn't the variable, in itself, that is used as the alias, but its name. What does it means?

QueryOver<Campaign> query;

{
    Customer custAlias = null;
    query = session.QueryOver<Campaign>()
         .JoinAlias(x => x.Customer, () => custAlias)
}

{
    Customer custAlias = null;
    var result = query.OrderBy(() => custAlias.Name).Desc()   // want to use string property name
       .List()

}

Two different custAlias, but it still works :-)

(useful if you want to split pieces of a query in multiple methods... The only important thing is that they use the same naming for the aliases)