我无法在任何地方找到它,但它似乎很微不足道。所以,请原谅这是否重复。
我有类似的东西:
public class Doctor : Entity
{
...some other properties here...
public virtual string Email { get; set; }
}
public class Lawyer : Entity
{
...some other properties here...
public virtual string Email { get; set; }
}
我希望在Lawyers表中返回所有没有电子邮件匹配的医生,如:
select * from Doctors d
where d.Email not in
(select l.Email from Lawyers l where l.Email is not null)
或使用联接:
select d.* from Doctors d
left join Lawyers l on l.Email = d.Email
where l.Email is null
问题是电子邮件当然不是设置为外键。我没有映射到Lawyer的Doctor实体的映射属性。
到目前为止我尝试过:
ICriteria criteria = Session.CreateCriteria(typeof(Doctor))
.CreateAlias("Lawyers.Email", "LawyerEmail", JoinType.LeftOuterJoin)
.Add(Restrictions.IsNull("LawyerEmail"));
return criteria.List<Doctor>();
但是,我得到了“无法解析MyPlatform.MyNamespace.Doctor的律师”错误。任何想法如何设置我的DoctorMap并调整标准tomfoolery来实现这一目标?
NHibernate的损失........实体框架的胜利......
答案 0 :(得分:1)
我们可以通过一个名为子查询的功能实现这一目标:
// a inner SELECT to return all EMAILs from Lawyer table
var subQuery = DetachedCriteria.For<Lawyer>()
.SetProjection(Projections.Property("Email"));
// the root SELECT to get only these Doctors
var criteria = session.CreateCriteria<Doctor>();
// whos email is not in the sub SELECT
criteria.Add(Subqueries.PropertyNotIn("Email", subQuery));
// get first 10
var result = criteria
.SetMaxResults(10)
.SetFirstResult(0) // paging
.List<Doctor>();