Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 270679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:02:26+00:00 2026-05-12T00:02:26+00:00

I’m having some trouble understanding a bit of code. I’ve got 2 classes Company

  • 0

I’m having some trouble understanding a bit of code. I’ve got 2 classes Company and CompanyLocation. Mapped as:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.DataTransfer.CompanyLocation,Core.DataTransfer" table="`CompanyLocation`" lazy="true">
    <id name="CompanyLocationId" column="`CompanyLocationID`" type="int">
      <generator class="native" />
    </id>
    <many-to-one name="Company" cascade="none" column="`CompanyID`" not-null="true" />
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.DataTransfer.Company,Core.DataTransfer" table="`Company`" lazy="true">
    <id name="CompanyId" column="`CompanyID`" type="int">
      <generator class="native" />
    </id>
    <property type="string" not-null="true" length="100" name="CompanyName" column="`CompanyName`" />
    <bag name="CompanyLocations" inverse="true" lazy="true" cascade="all">
      <key column="`CompanyID`" />
      <one-to-many class="Core.DataTransfer.CompanyLocation,Core.DataTransfer" />
    </bag>
  </class>
</hibernate-mapping>

I’m trying to query using a Criteria query as follows:

        public IList<CompanyLocation> GetCompanyLocations(string sortExpression)
        {
            ICriteria criteria = _session.CreateCriteria(typeof(CompanyLocation));

            if (!string.IsNullOrEmpty(sortExpression))
            {
                Sort sort = new Sort(sortExpression);
                if (!string.IsNullOrEmpty(sort.AssociationPath))
                {
                    criteria.CreateCriteria(sort.AssociationPath).AddOrder(new Order(sort.SortColumn, sort.IsAscending));
                }
//                if (!string.IsNullOrEmpty(sort.AssociationPath))
//                {
//                    criteria.CreateCriteria(sort.AssociationPath);
//                }
//                criteria.AddOrder(new Order(sort.SortColumn, sort.IsAscending));
            }
            return criteria.List<CompanyLocation>();
        }

When I call the method and pass in a sortExpression of “Company.CompanyName DESC” the Sort class will parse it so that:
AssociationPath = “Company”
SortColumn = “CompanyName”
IsAscending = false

The if block in the method that is not commented out works. If I change to the one that is commented out it fails with:
could not resolve property: CompanyName

I’m having trouble understanding why.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:02:27+00:00Added an answer on May 12, 2026 at 12:02 am

    Most likely because CreateCriteria() creates a new criteria (instead of modifying original one as your code expects)

    Try changing to

    if (!string.IsNullOrEmpty(sort.AssociationPath))
    {
       var criteriaCopy = criteria.CreateCriteria(sort.AssociationPath);
    }
    criteriaCopy.AddOrder(new Order(sort.SortColumn, sort.IsAscending));
    

    or just plain

    if (!string.IsNullOrEmpty(sort.AssociationPath))
    {
       criteria = criteria.CreateCriteria(sort.AssociationPath);
    }
    criteria.AddOrder(new Order(sort.SortColumn, sort.IsAscending));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.