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
– Guilherme Padovam
If this is Guilherme: http://sqlfiddle.com/#! 6/1d405/1
– Marconi
This Marconi is what I want, but how would it apply this in c#,
– Guilherme Padovam
Guilherme Generates some error in C#? Or does not return anything?
– Marconi
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
– Guilherme Padovam
Let’s go continue this discussion in chat.
– Marconi