Nhibernate With Clause Usage
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.开发者_如何学CSession.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.
var employeeNames = Session.CreateCriteria(typeof(Employee))
.CreateAlias("Chair", "c") // InnerJoin is implied
.Add(Restrictions.GtProperty("Weight", "c.MaxLoad"))
.SetProjection(Projections.Property("LastName"))
.List<string>();
The Restrictions.XxProperty()
constraints are what you are looking for, which compare two properties against each other.
精彩评论