如何在 NHibernate HQL 中使用 SUM?

How to use SUM in NHibernate HQL?

我正在尝试 SUM 使用 NHibernate 的 HQL,但是当执行查询时抛出异常 A first chance exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll 而结果不是 return。我该怎么做?

正在尝试。

public IList<Conta> findAllContasReceber() {
            ISession _session = getSession();
            IList<Conta> list = _session.CreateQuery("SELECT SUM(c.valorFinal) " + 
                                                     "FROM Conta c " + 
                                                     "WHERE (c.tipoConta = 1) AND (c.status = 0) " + 
                                                     "GROUP BY c.dtVencimento, c.cliente " + 
                                                     "ÖRDER BY c.dtVencimento ASC "
                                                     )                
                .List<Conta>();
            return list;
        }

实体

[Serializable]
    public class Conta {

        public virtual long id                      { set; get; }        
        public virtual Cliente cliente              { set; get; }
        public virtual String historico             { set; get; }
        public virtual DateTime dtLancamento        { set; get; }
        public virtual DateTime dtVencimento        { set; get; }
        public virtual decimal valorPagar           { set; get; } //total vendas
        public virtual decimal valorAcrescimo       { set; get; } //total acrescimo
        public virtual decimal valorFinal           { set; get; } //total pagar

        public virtual DateTime dtPagamento         { set; get; }
        public virtual int tipoConta                { set; get; }  //1 receber, 2 pagar
        public virtual PlanoDeConta planoConta      { set; get; }
        public virtual int status                   { set; get; } //0 ativa, 1 fechada, 2 cancelada, 3 aguardando pagamento
        public virtual Venda venda                  { set; get; }


        public Conta() {

        }
    }

已解决。在寻找了很多建议之后,我找到了一种方法来做我想做的事。

这里是解决方案。

public IList<Conta> findAllContasReceber() {
            ISession _session = getSession();
            String SQL = "SELECT new Conta(c.cliente, c.historico, c.dtLancamento, c.dtVencimento, SUM(c.valorPagar), SUM(c.valorAcrescimo), SUM(c.valorFinal), c.status) " + 
                         "FROM Conta c WHERE (c.tipoConta = 1) AND (c.status = 0) " + 
                         "GROUP BY c.cliente, c.dtVencimento " + 
                         "ORDER BY c.dtVencimento ";
            IList<Conta> list = _session.CreateQuery(SQL).List<Conta>();
            return list;
        }

然后我在classConta中创建了一个构造函数来接收参数

[Serializable]
    public class Conta {

        public virtual long id                      { set; get; }        
        public virtual Cliente cliente              { set; get; }
        public virtual String historico             { set; get; }
        public virtual DateTime dtLancamento        { set; get; }
        public virtual DateTime dtVencimento        { set; get; }
        public virtual decimal valorPagar           { set; get; } //total vendas
        public virtual decimal valorAcrescimo       { set; get; } //total acrescimo
        public virtual decimal valorFinal           { set; get; } //total pagar
        public virtual DateTime dtPagamento         { set; get; }
        public virtual int tipoConta                { set; get; }  //1 receber, 2 pagar
        public virtual PlanoDeConta planoConta      { set; get; }
        public virtual int status                   { set; get; } //0 ativa, 1 fechada, 2 cancelada, 3 aguardando pagamento
        public virtual Venda venda                  { set; get; }


        public Conta() {
        }

        public Conta(Cliente cliente, String historico, DateTime dtLancamento, DateTime dtVencimento, 
                    decimal valorPagar, decimal valorAcrescimo, decimal valorFinal, int status){

                        this.cliente = cliente;
                        this.historico = historico;
                        this.dtLancamento = dtLancamento;
                        this.dtVencimento = dtVencimento;
                        this.valorPagar = valorPagar;
                        this.valorAcrescimo = valorAcrescimo;
                        this.valorFinal = valorFinal;
                        this.status = status;
        }

    }

终于 100% 有效