It is bringing the whole column and not only a specific id

Asked

Viewed 61 times

0

I’m trying to catch the last mile registered on the seat of a specific vehicle, but it brings me all the miles 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 you get the bank information:

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 mile on the bench and then compare it to the current one, if it’s bigger it will register, if there’s no error

  • Guilherme you need to use the Id parameter as a filter in the Product Query method. Example: string hql = "SELECT Campox, Campoy FROM Abastecimento WHERE Campofkdeveiculo = " + Id;

  • I’m new to c#. net com mvc, I don’t know how to apply in hql the option of it interfering in my view, I usually apply the Where in the controller and use the viewmodel

1 answer

1


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

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

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

EDIT: Inclusíve I changed the value passed as parameter to 0 to demonstrate that it does not differ at all...

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 in other places.

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 in your controller using the model value

var Rota = ckm.ConsultaProduto(viewModel.NumCarroId)
  • This will make me appreciate the vehicle I desire for Tolist?

  • by Where()... but your Product Query method will remain wrong... you pass a parameter to it that should be the filter but uses nowhere...

  • the right would be apply the Where by hql then, so for it filters and leaves the process less slow, and filters correctly?

  • Exact, but Where no SQL, in the string statement hql

  • Start applying this in the system, I was seeing that many systems and exercises apply Where in hql, to have less process and speed, my problem is more do it takes the value of the view to Where does the filtering

  • A doubt, there in your ID column, can be foreign key or would have to implement something else?

  • [SUA_COLUNA_DE_ID] is the column of the Supply table that you want to compare if it is equal to the parameter Id that the method receives as input

  • I was able to perform the implantation, and only by touching this query, I already felt that the search process and add get faster

  • Make this review in all your methods of consulting the bank.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.