views:

15

answers:

1

Hi guys, I would like to ask on how you do ordering on an object with 2 properties that are basically joined from 2 entities.

Lets say "ClassA" has a property named ClassB and ClassC which are also classes themselves. ClassB has a property named Name and ClassC also has a property named Name. Now, since ClassB and ClassC are both properties of ClassA, how can i order a list of ClassAs using the Name properties of ClassB and ClassC. Below is the sample NHibernate criteria code in C#:

ICriteria criteria = m_Session.CreateCriteria<ClassA>()
     .AddOrder(Order.Asc("ClassB.Name"))
     .AddOrder(Order.Asc("ClassC.Name"));

But the above code throws an error saying that "could not resolve property: ClassB.Name of: ClassA". Any help would be appreciated. Thanks a lot.

A: 

You will need to alias the 2 associations (ClassB & ClassB) so that you can order by them.

ICriteria criteria = m_Session.CreateCriteria<ClassA>()
    .CreateAlias("ClassB", "b")
    .CreateAlias("ClassC", "c")
    .AddOrder(Order.Asc("b.Name"))
    .AddOrder(Order.Asc("c.Name"));
Lachlan Roche
exactly the answer! great thanks!
jerbersoft