0
I’m trying to do a c# query with Mysql, where there are two tables:
Compra
--> Table where you take the IdProduto
and the Product Value.
Abastecimento
--> Table where the IdProduto
and the Product Value for registration.
The query between these tables I have already been able to perform, where it brings me the values of the table Compra
, but it brings me the value of all products, what I wanted it to do would be to bring me the value of the selected product, an example would be more or less 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 brings the right ID and it was only to bring me the value of it, but it takes all values from the table Compra
and brings me the last recorded of the entire table, an example of how it returns is like this:
| Id | Valor | Produto |
| 3 | 4,7 | S-10 |
When the right one returns, it is so:
| Id | Valor | Produto |
| 3 | 3,4 | S-10 |
My Supply and Control:
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>();
}
Possible duplicate of Pick up values by dropdownlist
– Leandro Angelo
@Leandroangelo I had removed, and tried to do this in a more explained way
– Guilherme Padovam
ok... Face everything is confused in your code, structure, class names, methods and tables. Do an exercise and try to reproduce this scenario in real life or on paper, and only then start coding.
– Leandro Angelo
@Leandroangelo this is already on paper, and it works in a manual way, but I don’t understand the part where it is confused, because the classes and tables are with names of what will be used or where it is used, if it’s not clear to you, show me where you are and I will explain or change for better understanding.
– Guilherme Padovam
First, one minute you call it a buy, the next is a supply. Your Product Query method takes a parameter that is never used, called Product Name but is an integer which would indicate that it should be an ID, behind all the records of the Supply table for the Product var, which you later use to assign the value var by taking the higher value from the Supply table. Does that make sense? None of your SELECT has WHERE
– Leandro Angelo
@They even lost me here. You have a Consutlavalor method that takes the Vlrunit parameter, which is not used anywhere and returns all records from the Purchase table. You store this result in the var Product, and then in the var Value, you save the first orderly record down by the attribute Vlrunit that is not even represented in your tables. So it’s hard to understand and help.
– Leandro Angelo
There are two tables @Leandroangelo, it is very clear where I informed, where the purchase is where the values are registered and the supply will require this data, in the method Product Consultation is there if you need to use and the in it is clear that is the ID, because the name is Nomeprodutoid, where is a foreign Purchase key
– Guilherme Padovam