NHibernate的连接两个实体,并返回另一个实体

问题描述:

我有一个主实体Trnx这样的:NHibernate的连接两个实体,并返回另一个实体

public class MasterTrnx 
{ 
    private int? _AccountId; 
    private string _Place; 
    private DateTime _ProcessTime; 
    private int _TrnxId; 
    private decimal? _PaymentValue; 
    } 

和硕的子实体这样的:

public class MasterTrnxDetail 
    { 
    private MasterTrnx _MasterTrnx; 
    private decimal _MasterPaymentValue; 
    private decimal _PaymentValue; 
    private int _Xid; 
    } 

一个MasterTrnx实体比MasterTrnxDetail一个多儿童。

using (ISession session = base.GetSession()) 
     { 
      try 
      { 
       tx = session.BeginTransaction(); 

       listOfMasterTrnxDetail = session.QueryOver<MasterTrnxDetail>() 
        .JoinQueryOver(d => (IEnumerable<MasterTrnx>)d.Trnx) 
        .List(); 

       tx.Commit(); 
      } 
      catch (Exception e) 
      { 
       if (tx != null) 
       { 
        tx.Rollback(); 
       } 
       throw e; 
      } 
      finally 
      { 
       session.Close(); 
      } 

      return listOfMasterTrnxDetail; 
     } 

此代码块正在工作。例如,我有一个主实体,它有三个masterdetail。这段代码给了我3条记录。但我想要一个主记录和总计细节'MasterPaymentValues。我怎样才能做到这一点?此外,我想方法返回像这样的另一个实体:

public class Trnx 
{ 
    public decimal? PaymentValue { get; set; } 
    public DateTime ProcessTime { get; set; } 
    public string TrnxName { get; set; } 
    public decimal? TotalMasterPaymentValue { get; set; } 
} 

感谢您的帮助。

类模型似乎有点奇怪,所以我只能猜测

MasterTrnx mastertx = null; 

var subquery = QueryOver.Of<MasterTrnxDetail>() 
    .Where(detail => detail.MasterTrnx = mastertx) 
    .Select(Projections.Sum<MasterTrnxDetail>(d => d.PaymentValue)); 


Trnx dto = null; 

transactions = session.QueryOver(() => mastertx) 
    .SelectList(list => list 
     .Select(t => t.ProcessTime).WithAlias(() => dto.ProcessTime) 
     .Select(t => t.Place).WithAlias(() => dto.TrnxName) 
     .Select(Projections.Subquery(subquery)).WithAlias(() => dto.TotalMasterPaymentValue) 
    ) 
    .TransformUsing(Transformers.AliasToBean<Trnx>()) 
    .List<Trnx>();