在nHibernate的Criteria中编写子查询

发布于 2024-09-01 00:48:29 字数 321 浏览 7 评论 0原文

我已经在 Criteria 中阅读了有关子查询的内容,但我仍然无法正确掌握它。在这里我举一个例子,如果有人可以帮助我使用子查询来编写它,那就太好了。

假设我们有一张桌子,

Employee{EmployeeId.(int),Name(string),Post(string),No_Of_years_working(int)}

现在我想要所有担任经理且工作时间不超过 10 年的员工。我知道我们可以在不使用子查询的情况下获得结果,但我想使用子查询只是为了了解它在条件中的工作原理。

那么,我如何使用子查询编写条件来获取这些员工。

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.

Lets say we have table

Employee{EmployeeId.(int),Name(string),Post(string),No_Of_years_working(int)}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥繁 2024-09-08 00:48:29

嗯 - 代码应该是这样的:

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"))
);

希望这会有所帮助。

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"))
);

Hope this helps.

千笙结 2024-09-08 00:48:29

当我发现这个问题时,我试图执行类似于 Bipul 的任务,所以我主要得到 bernhardrusch 的答案想法,但我意识到,如果不添加 Projections.projectionList 子查询将无法工作。所以我决定在最终版本中删除几行代码:

Session session; //You get the session according with your app logic

//Let's define first the subquery
DetachedCriteria sub = DetachedCriteria.forClass(Employee.class);
sub.add( Restrictions.lt("No_Of_years_working", 10) );
sub.add( Restrictions.eq("Post", "Manager") );
sub.setProjection( 
Projections.projectionList().add(                   Projections.property("EmployeeId") 
) 
);

//Now the main query
Criteria criteria = session.createCriteria(Employee.class);
criteria.add( Property.forName("EmployeeId").in(sub) ); 

I was trying to perform something similar to Bipul's task, when I found this question, so I mainly got bernhardrusch's answer idea, but I've realized that without adding the Projections.projectionList the subquery do not work. So I've decided to drop a few lines of code with the final version:

Session session; //You get the session according with your app logic

//Let's define first the subquery
DetachedCriteria sub = DetachedCriteria.forClass(Employee.class);
sub.add( Restrictions.lt("No_Of_years_working", 10) );
sub.add( Restrictions.eq("Post", "Manager") );
sub.setProjection( 
Projections.projectionList().add(                   Projections.property("EmployeeId") 
) 
);

//Now the main query
Criteria criteria = session.createCriteria(Employee.class);
criteria.add( Property.forName("EmployeeId").in(sub) ); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文