【问题标题】:How To Join Two Tables In Nhibernate如何在 Nhibernate 中加入两个表
【发布时间】:2015-04-28 14:52:42
【问题描述】:
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[]>();

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

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

提前致谢。

【问题讨论】:

  • 当您使用纯 SQL 时,您希望使用 ADO.NET 而不是 nHibernate。 nHibernate 是当您不想自己编写 SQL 时。
  • @Euphoric 是的,我能够运行 IQuery 并转换为列表。

标签: c# nhibernate inner-join


【解决方案1】:

这很容易。出乎意料的容易。检查

因此,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[]>();

【讨论】:

  • 这就是答案
猜你喜欢
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 2021-07-23
  • 1970-01-01
  • 2010-11-17
  • 2021-06-25
相关资源
最近更新 更多