nhibernate queryover与子查询连接以获取聚合列

时间:2011-08-22 21:27:37

标签: nhibernate subquery queryover

我现在一直在寻找几个小时如何做到这一点,但似乎无法找到任何帮助我。

以下是数据库模型:

enter image description here

这是我尝试运行的SQL查询:

SELECT b.*, a.Assignments FROM Branch b LEFT JOIN (
        SELECT b.BranchID , COUNT(ab.BranchID) AS Assignments
        FROM Branch b LEFT JOIN AssignmentBranch ab ON b.BranchID = ab.BranchID
        GROUP BY b.BranchID
      ) a ON b.BranchID = a.BranchID

所以,基本上,我想返回一个分支列表和一个新列,表示该分支的赋值数。

分支模型

public class Branch : IEntity<int>
{
    public virtual int ID
    {
        get;
        set;
    }

    public virtual string Name { get; set; }

    public virtual IList<AssignmentBranch> Assignments { get; set; }

}

AssignmentBranch模型

public class AssignmentBranch : IEntity<int>
{
    public virtual int ID
    {
        get;
        set;
    }

    public virtual DateTime AssignedOn { get; set; }

    public virtual Branch Branch { get; set; }
}

这是我的NHibernate配置:

<class name="Branch" table="Branch">

<id name="ID" column="BranchID">
  <generator class="identity"></generator>
</id>

<property name="Name"/>

<bag name="Assignments" cascade="none" inverse="true">
  <key column="BranchID"/>
  <one-to-many class="AssignmentBranch"/>
</bag>

 <class name="AssignmentBranch" table="AssignmentBranch">

<id name="ID" column="AssignmentBranchID">
  <generator class="identity"></generator>
</id>

<property name="AssignedOn" />
<property name="FromDate" />
<property name="ToDate" />

<many-to-one name="Assignment" column="AssignmentID" />
<many-to-one name="Branch" column="BranchID" />

我尝试了很多种方法,但我似乎找不到使用QueryOver加入子查询的方法。

我试过这样:

 // aliases
 Branch branch = null; AssignmentBranch assignment = null;

 var subquery = QueryOver.Of<Branch>(() => branch)
     .Where(() => branch.Project.ID == projectID)
     .JoinQueryOver<AssignmentBranch>(() => branch.Assignments, ()=> assignment, 
                                   NHibernate.SqlCommand.JoinType.LeftOuterJoin)
     .SelectList(list => list
                        .SelectGroup(x=>x.ID)
                        .SelectCount(()=>assignment.ID)
                    );

     var query = session.QueryOver<Branch>(()=>branch)
                  .JoinAlias(???) // how can I join with a sub-query?
                  .TransformUsing(Transformers.AliasToBean<BranchAssignments>())
                  .List<BranchAssignments>();

有人能帮帮我吗?它不一定与子连接完全相同,也许还有另一个更好的解决方案,我错过了...

谢谢你, 科斯明

2 个答案:

答案 0 :(得分:12)

在这里阅读了数百个类似的问题之后,我找到了答案:一个相关的子查询。像这样:

// aliases
Branch branch = null; AssignmentBranch assignment = null;

var subquery = QueryOver.Of<AssignmentBranch>(() => assignment)
    .Where(() => assignment.Branch.ID == branch.ID)
    .ToRowCountQuery();

var query = session.QueryOver<Branch>(() => branch)
     .Where(() => branch.Project.ID == projectID)
     .SelectList
     (
         list => list
         .Select(b => b.ID)
         .Select(b => b.Name)
         .SelectSubQuery(subquery)
     )
     .TransformUsing(Transformers.AliasToBean<BranchAssignments>())
     .List<BranchAssignments>();

我得到答案的类似问题是this one

答案 1 :(得分:2)

使用QueryOver并不容易,因为目前不可能在FROM子句中包含语句。我想到的一件事(不是我认为最有效的方式)

var branches = session.QueryOver<Branch>().Future();

var assignmentMap = session.QueryOver<BranchAssignment>()
    .Select(
        Projections.Group<BranchAssignment>(ab => ab.Branch.Id).As("UserId"),
        Projections.RowCount())
    .Future<object[]>()
    .ToDictionary(o => (int)o[0], o => (int)o[1]);

return branches.Select(b => new { Branch = branch, AssignmentCount = assignmentMap[branch.Id] });
使用LINQ

将是

var branchesWithAssignementCount = session.Query<Branch>()
    .Select(b => new { Branch = b, AssignmentCount = b.Branch.Count })
    .ToList();