How to use the SUM aggregation function in a NHIBERNATE query?

Asked

Viewed 528 times

2

My consultation consists in knowing the NumCarroId, TotalGasto and VlrUnit, in Sql I managed to do that but when I play in HQL returns the following error:

Application of GROUP BY next to the SUM to make a Tolist, to show me a list already added, but there are many errors

Personal if anyone can help, for now I have not found any answer that will work

Error that returns:

string hql = "select a.NumCarro.Id, sum(a.Totalspent) spent from Sourcing to WHERE a.Dtfuelled Between :dateInitial AND :dataFinal GROUP BY a.NumCarro.Id";

Code

public IList<Abastecimento> Resumo(DateTime dataInicio, DateTime dataFinal)
{
    string hql = "SELECT NumCarroId, DtAbastecido FROM Abastecimento a WHERE a.DtAbastecido Between :dataInicial AND :dataFinal GROUP BY a.NumCarroId";
    IQuery query = session.CreateQuery(hql)
    .SetParameter("dataInicial", dataInicio)
    .SetParameter("dataFinal", dataFinal);
    return query.List<Abastecimento>();
}

Table Supply

CREATE TABLE [dbo].[Abastecimento] (
    [Id]            INT             IDENTITY (1, 1) NOT NULL,
    [DtAbastecido]  DATETIME        NULL,
    [Litro]         INT             NULL,
    [VlrUnit]       DECIMAL (18, 2) NULL,
    [TotalGasto]    DECIMAL (18, 2) NULL,
    [AutorId]       INT             NULL,
    [NumCarroId]    INT             NULL,
    [Km]            INT             NULL,
    [NomeProdutoId] INT             NULL,
    [Km_Andado]     INT             NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK84286500787E6DCB] FOREIGN KEY ([AutorId]) REFERENCES [dbo].[Usuario] ([Id]),
    CONSTRAINT [FK842865002F58EAD8] FOREIGN KEY ([NumCarroId]) REFERENCES [dbo].[Veiculo] ([Id]),
    CONSTRAINT [FK8428650023D2F9D9] FOREIGN KEY ([NomeProdutoId]) REFERENCES [dbo].[Compra] ([Id])
);

Model Abastecimento

 public class Abastecimento {
 public virtual int Id {
  get;
  set;
 }
 [Required]
 public virtual int Litro {
  get;
  set;
 }
 public virtual DateTime ? DtAbastecido {
  get;
  set;
 }
 public virtual decimal VlrUnit {
  get;
  set;
 }
 public virtual int Km {
  get;
  set;
 }
 public virtual decimal TotalGasto {
  get;
  set;
 }
 public virtual int Km_Andado {
  get;
  set;
 }
 public virtual Usuario Autor {
  get;
  set;
 }
 public virtual Compra NomeProduto {
  get;
  set;
 }
 public virtual Veiculo NumCarro {
  get;
  set;
 }
}
}
  • By the test I was doing in sql, doing so he brought the cars I needed and in the period, I used the sum to make the sum, only I did not put there in the query because it was giving a gigantic error

  • If this is Guilherme: http://sqlfiddle.com/#! 6/1d405/1

  • This Marconi is what I want, but how would it apply this in c#,

  • Guilherme Generates some error in C#? Or does not return anything?

  • are two mistakes that happen is what I put and if I try to implement the sum is one that I will put there in the question

Show 1 more comment

2 answers

4

For your mistake, that question: How to resolve No data type for Node error in Hibernate, says:

An HQL Query must contain the object properties and not the fields of your table structure.

In SQL:

select numCarroId, sum(valorGasto) gastos from Abastecimento
where dataAbastecimento between '2017-05-21' and '2017-11-13'
group by numCarroId

Sqlfiddle

You gave it to me via chat Model Abastecimento thus:

public class Abastecimento {
 public virtual int Id {
  get;
  set;
 }
 [Required]
 public virtual int Litro {
  get;
  set;
 }
 public virtual DateTime ? DtAbastecido {
  get;
  set;
 }
 public virtual decimal VlrUnit {
  get;
  set;
 }
 public virtual int Km {
  get;
  set;
 }
 public virtual decimal TotalGasto {
  get;
  set;
 }
 public virtual int Km_Andado {
  get;
  set;
 }
 public virtual Usuario Autor {
  get;
  set;
 }
 public virtual Compra NomeProduto {
  get;
  set;
 }
 public virtual Veiculo NumCarro {
  get;
  set;
 }
}
}

Modify your HQL to:

    public IList < Abastecimento > Resumo(DateTime dataInicio, DateTime dataFinal) {
  string hql = "select a.NumCarro.Id, sum(a.TotalGasto) gastos from Abastecimento a WHERE a.DtAbastecido Between :dataInicial AND :dataFinal GROUP BY a.NumCarro.Id";
  IQuery query = session.CreateQuery(hql)
   .SetParameter("dataInicial", dataInicio)
   .SetParameter("dataFinal", dataFinal);
  return query.List < Abastecimento > ();
 }
  • I answered the question based on error, my intention is that the OP understand a little of the problem. If anyone knows of NHibernate and can contribute I remove my response.

  • The chat has a lot of information: https://chat.stackexchange.com/rooms/71825/discussion-between-marconi-and-guilherme-padovam

  • 1

    at first Nhibernate is right... but I wouldn’t use that ORM... it’s too slow

  • What would be a better ORM?

  • the Entity Framework is already better, but if you need performance use the Dapper. That in your case would be much better since you do not use.... Dapper would be fine.

  • @Guilhermepadovam, Which error when executing the hql of this answer? Error would not be in the return parse of the List? Because how the ORM will know which property sum(a.TotalGasto) gastos refers to the return?

  • @Guillermo, look how this answer was implemented: https://stackoverflow.com/a/39884363/2290538

  • I’ll look and if I can put the answer

  • @Fernandoleal good analyzed, I believe my answer lacks some detail.

Show 4 more comments

1


I put this next line of code in the controller, and I was able to add Group By, but I still can’t do SUM

var teste = consulta.Where(i => i.DtAbastecido >= dataInicio && i.DtAbastecido <= dataFinal)
                    .GroupBy(x => new { x.NumCarro.NCarro})
                    .Select(x => x.First())
                    .OrderBy(x => x.NumCarro.NCarro);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.