I'm trying to make a query through C # with MySQL, where it has two tables:
Compra
- > Table where you get the IdProduto
and the Product Value.
Abastecimento
- > Table where the IdProduto
will go and the Product Value for registration.
The query between these tables I've already been able to do, where it brings me the values of the Compra
table, but it brings me the value of all the products, what I wanted it to do would be to bring me the value of the product selected, an example would look something like this.
|------------ Compra --------------|
| Id | Valor | Produto |
| 1| 4,2 | S-500 |
| 2| 3,1 | S-10 |
| 3| 3,4 | S-10 |
| 4| 2,3 | Etanol |
| 5| 4,7 | Gasolina Aditivada |
When I select the S-10 in the supply form, it returns the right ID and was only to bring me the value of it, but it takes all the values from the Compra
table and brings me the last registered table An example of how it returns is like this:
| Id | Valor | Produto |
| 3 | 4,7 | S-10 |
Since the correct a returns, it is like this:
| Id | Valor | Produto |
| 3 | 3,4 | S-10 |
My SupplyController:
public ActionResult Adiciona(AbastecimentoModel viewModel)
{
var Produto = ckm.ConsultaValor(viewModel.NomeProdutoId);
var Valor = Produto.OrderByDescending(c => c.VlrUnit).First();
viewModel.VlrUnit = Valor.VlrUnit;
viewModel.TotalGasto = viewModel.VlrUnit * viewModel.Litro;
if (ModelState.IsValid)
{
Abastecimento abastecimento = viewModel.CriaAbastecimento();
dao.Adiciona(abastecimento);
//return View();
return RedirectToAction("Index");
}
else
{
ViewBag.Compra = compraDAO.Lista();
ViewBag.Usuarios = usuarioDAO.Lista();
ViewBag.Veiculo = veiculoDAO.Lista();
return View("Form", viewModel);
}
My query query:
public IList<Abastecimento> ConsultaProduto(int NomeProdutoId)
{
string hql = "SELECT a FROM Abastecimento a";
IQuery query = session.CreateQuery(hql);
return query.List<Abastecimento>();
}
public IList<Compra> ConsultaValor(float VlrUnit)
{
string hql = "SELECT c FROM Compra c";
IQuery query = session.CreateQuery(hql);
return query.List<Compra>();
}