如何在Nhibernate中加入两个表

时间:2015-02-26 09:40:04

标签: c# nhibernate inner-join

List<object[]> olist = null;

olist = (_session.CreateQuery("Select pc.Id as Id,pct.DescEn as DescEn,pct.DescAr as DescAr,pc.ContentEn as ContentEn,pc.ContentAr as ContentAr from ProjectCharter pc,ProjectCharterTemplate pct where pct.Id=pc.PRC_PCT_ID and pc.PRC_PRJ_ID=1").List<object[]>()).ToList<object[]>();

这是我的查询,我想加入两个表并获得输出, 当我运行这是db我得到完美的答案, 但是当我使用nhibernate映射通过c#运行它时。我得到错误。

我可以这样查询,还是有其他方法可以连接两个表。

提前致谢。

1 个答案:

答案 0 :(得分:5)

这很容易。非常容易。检查

因此,QueryOver中的上述查询可能如下所示:

// alias for later use
ProjectCharter project = null;
ProjectCharterTemplate template = null;

var list = session
    .QueryOver<ProjectCharter>(() => project)
    // the JOIN will replace the WHERE in the CROSS JOIN above
    // it will be injected by NHibernate based on the mapping
    // relation project has many-to-one template
    .JoinQueryOver<ProjectCharterTemplate>(c => c.Templates, () => template)
    .Select(
        // select some project properties
        _ => project.ContentEnglish,
        ...
        // select some template properties
        _ => template.DescriptionEnglish,
     )
    .List<object[]>();
相关问题