views:

267

answers:

1

I'm trying to create a query using NHibernate and searching along multiple foreign keys:

The following code works when I'm only searching on one of the foreign keys:

      ICriteria query = Session.CreateCriteria<TblTeam>()
   .Add<TblTeam>(x => x.FldUrlSafeName == teamName)
   .CreateCriteria<TblTeam>(x => x.TblSportsType)
   .Add<TblSportsType>(x => x.FldUrlSafeName == sportsType);


  return query.UniqueResult<TblTeam>();

I need to search on two of the foreign keys so I created the following:

      ICriteria query = Session.CreateCriteria<TblTeam>()
   .Add<TblTeam>(x => x.FldUrlSafeName == teamName)
   .CreateCriteria<TblTeam>(x => x.TblSportsType)
   .Add<TblSportsType>(x => x.FldUrlSafeName == sportsType)
   .CreateCriteria<TblTeam>(x => x.TblSportsOrganization)
   .Add<TblSportsOrganization>(x => x.FldUrlSafeName == sportsOrganization);

  return query.UniqueResult<TblTeam>();

Unfortunately, this gives me the following error:

"could not resolve property: TblSportsOrganization of: site.Core.TblSportsType"

How would I have it take a "step back" as it were so I can add criteria to TblTeam and not TblSportsType?

+1  A: 

After talking with some people in IRC I've found an answer. You need to hold on to the reference to the original ICriteria. It should have been obvious.

      ICriteria query = Session.CreateCriteria<TblTeam>();
  query.Add<TblTeam>(x => x.FldUrlSafeName == teamName);

  query.CreateCriteria<TblTeam>(x => x.TblSportsType)
   .Add<TblSportsType>(x => x.FldUrlSafeName == sportsType);

  query.CreateCriteria<TblTeam>(x => x.TblSportsOrganization)
   .Add<TblSportsOrganization>(x => x.FldUrlSafeName == sportsOrganization);

  return query.UniqueResult<TblTeam>();