Monday, March 28, 2011

NHibernate AddOrder on related entity property

I have a search form and result list. The form allows the user to search on the entity properties and related entity properties.

NameContains EmailContains CompanyNameContains

The result list displays the contact properties and the relevant related properties, in this case company name

Name | Phone | Email | Company

Company is a relation and for this scenario the relation is eager loaded. Everything is working fine for the search side, filtered results, paging, etc.

I'm using DetachedCriteria and I can only get the sorting to work on Contact properties. (actually don't need sorting on the emails and phone numbers really). I am having trouble sorting on Company Name which is the related property. I wondered if anyone could point me in the right direction. Thanks in advance for you assistance.

From stackoverflow
  • Hmm, I would think that something like this should do the trick (the code below is not verified for syntax, since I was just about to shut down my pc, and I didn't wanted to restart VS.NET (lazy)

    DetachedCriteria crit = DetachedCriteria.For<Contact>();
    ...
    crit.CreateAlias ("Company", "c");
    crit.AddOrder (Order.Asc("c.CompanyName"));
    

    Something like that ? At least, that is what I would try ...

    M. Mather : Thank you... that was exactly what it was. BTW - Lazy usually equals efficient - thanks for taking the time.

0 comments:

Post a Comment