在 nHibernate 的 Criteria 中编写子查询 [英] Write subquery in Criteria of nHibernate

查看:35
本文介绍了在 nHibernate 的 Criteria 中编写子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 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 的 Criteria 中编写子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆