0

我有一个像这样的主实体 Trnx:

 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;
        }

此代码块正在工作。例如,我有一个主实体,它有 3 个三个主详细信息。这段代码给了我 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; }
}

感谢帮助。

4

1 回答 1

0

你的班级模型似乎有点奇怪,所以我只能猜测

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>();
于 2012-04-26T10:38:26.257 回答