You are bringing the entire column and not just a specific id

0

I'm trying to get the last km registered in the bank of a specific vehicle, but it brings me all km and not a specific vehicle.

My Controller:

 var Rota = ckm.ConsultaProduto(viewModel.NumCarroId);

        var maiorRota = Rota.OrderByDescending(c => c.Km).First();

            if (maiorRota != null && viewModel.Km < maiorRota.Km)
            // Aqui se não tiver valor para fazer comparação (maiorRota != null), ele ira registrar.
            // Ele ira fazer a comparação e ira salvar se estiver de acordo(viewModel.Km < maiorRota.Km).
            {
                ModelState.AddModelError("Km_Atual.Invalido", "A quilometragem precisa ser maior que a anterior");
            }

My query where to get the information from the bank:

public IList<Abastecimento> ConsultaProduto(int Id)
    {
        string hql = "SELECT a FROM Abastecimento a";
        IQuery query = session.CreateQuery(hql);
        return query.List<Abastecimento>();
    }

I'm trying to get him to take the last km in the bank and then compare with current, if he is older he will log, if there will not be an error

    
asked by anonymous 13.12.2017 / 17:48

1 answer

1

As @Renan commented you are not filtering your query, listing all the results and then selecting the one with the highest [Km] ... I already pointed this out in another question of yours.

Or you change your query method or make the filter in the controller, as you prefer ...

var Rota = ckm.ConsultaProduto(0)
              .Where(x=> x.Id == viewModel.NumCarroId).ToList();
  

EDIT:   I even changed the value passed as parameter to 0 to demonstrate that it does not make any difference ...

Now correcting your query method, but it may impact the rest of your application ... I have no way of knowing where you are using it elsewhere.

public IList<Abastecimento> ConsultaProduto(int Id)
{
    string hql = "SELECT a FROM Abastecimento a WHERE [SUA_COLUNA_DE_ID] ="+ Id;
    IQuery query = session.CreateQuery(hql);
    return query.List<Abastecimento>();
}

Now you can call the method on your controller using the value of the model

var Rota = ckm.ConsultaProduto(viewModel.NumCarroId)
    
13.12.2017 / 20:29