通过比较两列上的相等值来连接两个表

时间:2011-01-26 13:07:30

标签: nhibernate castle-activerecord nhibernate-criteria

为简单明了,假设我有这三个表。

员工 [ ID ]

EmployeeName [ 员工ID, 名称 ]

EmployeeAddress [ 员工ID, 地址 ]

定义的唯一属性/关系位于'子表'EmployeeName& EmployeeAddress

  • EmployeeName BelongsTo Employee
  • EmployeeAddress BelongsTo Employee

主Employee表中没有属性/关系(集合),例如

  • 员工HasMany EmployeeName
  • 员工HasMany EmployeeAdress。

我想在EmployeeName和EmployeeAddress之间使用DetachedCriteria执行联接 (不涉及员工),因此

  

选择EmployeeName.name,EmployeeAddress.address
  来自EmployeeName内部联接EmployeeAddress
  EmployeeName.employeeId = EmployeeAddress.employeeId和         EmployeeAddress.address,如'%somelocation%',按EmployeeAddress.address

排序

1 个答案:

答案 0 :(得分:1)

不完全是你正在寻找的查询,并且没有测试它是否确实有效..但它可能对你有帮助:

var addressCriteria = DetachedCriteria.For<EmployeeAddress>("ea")
                                      .Add(Restrictions.Like("ea.Address", "somelocation", MatchMode.Anywhere));
var nameCriteria = DetachedCriteria.For<EmployeeName>("en")
                                   .Add(Subqueries.PropertyIn("en.Employee", addressCriteria))
                                   .SetProjection(Projections.Property("en.Name"));