【问题标题】:Fluent Nhibernate join on non foreign key propertyFluent Nhibernate 加入非外键属性
【发布时间】:2014-08-01 19:21:03
【问题描述】:

我在任何地方都找不到这个,但它看起来很简单。所以,如果这是重复的,请原谅。

我有类似的东西:

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

问题是电子邮件当然没有设置为外键。我在 Doctor 实体上没有映射到 Lawyer 的映射属性。

到目前为止我已经尝试过:

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 为输........Entity Framework 为赢....

【问题讨论】:

    标签: nhibernate orm fluent-nhibernate foreign-keys left-join


    【解决方案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>();
    

    【讨论】:

    • 太棒了,享受 NHibernate,很棒的工具 :)
    猜你喜欢
    • 2011-08-31
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多