0

I have a mant-to-many relationship modeled in the database (with a bridge table) between Student and Professor (_students_selected) , in my entites i have modeled it as a many-to-many relationship i.e. a Professor has many Students.

HasManyToMany(x => x.Students)
   .Table("_students_selected").ChildKeyColumn("student_key").ParentKeyColumn("professor_key");

public class Professor    
{
        private IList<Students> _students;
        public virtual Student Students
        {
            get { return _students; }
            set { _students = value; }
        }
}

I am unable to query over the professors students, i have tried the following however nhibernate does not recognise Any to filter through the list. Whats the equivalent to any?

_unitOfWork.Session.QueryOver<Professor>()
   .Where(x => x.Students.Any(i => i.Id.IsIn(childStudentList))).List();
super ted
  • 21
  • 2
  • have you tried `.WithSubquery.Where(x => x.Students.Any(i => i.Id.IsIn(childStudentList)));`? – Firo Sep 16 '11 at 10:30
  • Hi firo thanks for the reply, i have just tried the following: _unitOfWork.Session.QueryOver().WithSubquery.Where( x => x.Students.Any(i => i.Id.IsIn(childStudentList))).List(); and im getting the following error 'Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.BinaryExpression'.' – super ted Sep 16 '11 at 12:56

1 Answers1

1

This helped me with a similar problem: nhibernate queryover not loading eagerly with a many to many joinalias

I was able to reduce it to:

Role role = null;

session.QueryOver<User>()
    .JoinAlias(u => u.Roles, () => role)
    .Where(() => role.Id == someId);
Community
  • 1
  • 1
Benjamin
  • 33
  • 1
  • 4