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

  • SEARCH
  • Home
  • 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 7021035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:23:48+00:00 2026-05-27T23:23:48+00:00

I have a table Team and I have a table Location. Each team can

  • 0

I have a table Team and I have a table Location. Each team can have multiple locations and each location could have multiple teams, so it is a many to many relationship.

When this scenario is created in the database, there will be 3 tables, Team, Location and TeamLocation to hold the many to many links.

I have mapped this in nhibernate using the following…
Team.hbm.xml

<class name="App.Data.Entities.Team,App.Data.Entities" table="`Team`" lazy="true">
   <id name="Teamid" column="`TeamID`" type="Guid">
      <generator class="assigned" />
    </id>
    <property type="string" not-null="true" length="250" name="TeamName" column="`TeamName`" />
    <bag name="TeamLocations" inverse="false" table="`TeamLocations`" lazy="true" cascade="all">
      <key column="`TeamID`" />
      <many-to-many class="App.Data.Entities.Location,App.Data.Entities">
        <column name="`LocationID`" />
      </many-to-many>
    </bag>
    </class>

and Location.hbm.xml

<class name="App.Data.Entities.Location,App.Data.Entities" table="`Location`" lazy="true">
    <id name="Locationid" column="`LocationID`" type="int">
      <generator class="native" />
    </id>
    <property type="string" not-null="true" length="250" name="LocationName" column="`LocationName`" />
    <property type="string" length="1000" name="Address" column="`Address`" />
    <bag name="Teams" inverse="false" table="`TeamLocations`" lazy="true" cascade="all">
      <key column="`LocationID`" />
      <many-to-many class="App.Data.Entities.Team,App.Data.Entities">
        <column name="`TeamID`" />
      </many-to-many>
    </bag>
  </class>

Now I am trying to retrieve all teams which are based in a specific location using criteria API, which I am unable to..

Equivalent sql…

SELECT     Team.TeamName, Location.LocationName
FROM         Team INNER JOIN
                      TeamLocations ON Team.TeamID = TeamLocations.TeamID INNER JOIN
                      Location ON TeamLocations.LocationID = Location.LocationID
WHERE     (TeamLocations.LocationID = 2) 

and in asp.net code

ICriteria criteria = _session.CreateCriteria(typeof(Team)).SetFirstResult(startRowIndex).SetMaxResults(maximumRows);

if (deptID > 0)
{
    Department dept = new Department(deptID);
    criteria.Add(Expression.Eq(Team.PropertyNames.Department, dept)); //for department, which works fine.
}

if (locationIDs.Count > 0)
{
    List<Location> locations = new List<Location>();

    foreach (int locationID in locationIDs)
    {
        Location loc = new Location(locationID);
        locations.Add(loc);
    }

    criteria.CreateCriteria("TeamLocations").Add(Expression.Eq(""TeamLocations"", locations));
}

return criteria.List<Team>();

This throws and error..

could not resolve property: TeamLocations of: App.Data.Entities.Location 

which I cant seen to figure out 🙁

Correct me..
Thanks!

  • 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-27T23:23:49+00:00Added an answer on May 27, 2026 at 11:23 pm

    You can add a restriction to an association path using CreateCriteria or CreateAlias as I did below. I would have to know more about your model and mapping to understand why you can add a restriction on Team.PropertyNames.Department without aliasing it.

        ICriteria criteria = _session.CreateCriteria(typeof(Team)).SetFirstResult(startRowIndex).SetMaxResults(maximumRows);
    
    if (deptID > 0)
    {
        Department dept = new Department(deptID);
        criteria.Add(Expression.Eq(Team.PropertyNames.Department, dept)); //for department, which works fine.
    }
    
    if (locationIDs.Count > 0)
    {
        // you may need to convert locationIds to an ICollection e.g.
        // var ids = locationIds.ToArray();
        criteria.CreateAlias("TeamLocations", "teamLocations");
        criteria.Add(Restrictions.In("teamLocations.LocationId", locationIds));
    }
    
    return criteria.List<Team>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some tables that benefit from many-to-many tables. For example the team table.
I have users , who are members of many teams through a membership table.
I have a model like this: Client - which has many Locations - which
I have the following two tables (simplified for this question): CREATE TABLE team (
I’m using SQL Server 2008. I have data as in this table: Team Email
I have table with 50 entries (users with such details like Name Surname Location
I have table with some fields that the value will be 1 0. This
I have this scenario and I'm not sure exactly how it should be modeled
I have two tables: players (has a team name column) and teams (also has
I have two table in my database. One called person and one called team.

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.