How to make a query bringing me the last value of a specific id

0

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>();
    }
    
asked by anonymous 09.11.2017 / 18:53

1 answer

1

This is not the solution to your problem, but the answer to your question.

Considering that the Purchase table is a repository or auxiliary table (Product_Value) where you save the registered values for your products:

If you want to get the last value entered for your product, without touching anything in its structure ...

var Valor = Produto.Where(p => p.Produto == viewModel.NomeProdutoId)
                    .OrderByDescending(c => c.id)
                    .FirstOrDefault();

* Replace p.Product with the correct name of your attribute

    
10.11.2017 / 13:21