在nHibernate的条件中编写子查询 [英] Write subquery in Criteria of nHibernate
问题描述
我已经了解了Criteria中的子查询,但是仍然无法正确理解它.在这里我以一个例子为例,如果有人可以帮助我使用子查询来编写它,那就太好了.
I've read about subquery in Criteria, but I am still unable to grasp it properly. Here I am taking one example and if somebody can help me write that using subquery it will be great.
让我们说我们有桌子
Employee{EmployeeId.(int),Name(string),Post(string),No_Of_years_working(int)}
现在,我希望所有经理人以及不到10年的工作人员.我知道无需使用子查询就可以得到结果,但是我想使用子查询只是为了了解其在条件中的工作方式.
Now I want all the employees who are Managers and working for less than 10 years. I know that we can get the result without using subqueries but I want to use subquery just to understand how it works in criteria.
因此,我该如何使用子查询编写条件来获取这些员工.
So, how I can write Criteria using subquery to get those employees.
推荐答案
嗯-代码应该是这样的:
Well - the code should be something like this:
DetachedCriteria dc = DetachedCriteria.For<Employee>()
.Add (Subqueries.PropertyIn("EmployeeId",
DetachedCriteria.For<Employee>()
.SetProjection(Projections.Property("EmployeeId"))
.Add(Restrictions.Lt("No_Of_years_working", 10))
.Add(Restrictions.Eq("Post", "Manager"))
);
希望这会有所帮助.
这篇关于在nHibernate的条件中编写子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!