I am trying to reproduce a query using the Nhibernate Criteria API and cannot work out how to add a criteria clause that allows me to compare two values from different tables.
The best trivial example I could come up with….
SELECT e.LastName
FROM Employee e
JOIN Chair c ON c.ChairId = e.ChairId
WHERE e.Weight > c.MaxLoad
My basic Nhibernate criteria
ICriteria criteria = base.Session.CreateCriteria(typeof(Employee));
criteria.CreateAlias("Employee.Chairid", "Chair", JoinType.InnerJoin);
One overload of create alias has an additional parameter of “withClause” which seems to be the suggested way to achieving this but for the life of me I cannot find an example of the syntax I would need to achieve this.
I think I need something along the lines of …
criteria.Add(Expression.Ge("Employee.Weight", "Chair.MaxLoad"));
but this obviously doesn’t work as the second parameter will be processed as a string value.
Any help appreciated.
The
Restrictions.XxProperty()constraints are what you are looking for, which compare two properties against each other.