Place the value of an hql on a var

Asked

Viewed 67 times

1

I need to get my two tables to work together, where the Abastecimento take the Product Name(NomeProdutoId), table Compra, I managed to get him to take the Product Name, but I wanted him to bring me the Value of that Product, and register it in the table Abastecimento, an example would be those registration forms, where you inform your state and just below behind only cities of that state.

But in mine I wanted the value, and the last recorded value, of that product.

That part was an attempt where in another form used a scheme to bring me a vehicle and then compare the KM.

If you need to change the query and the controller I can, or put foreign key, no problem.

Model Compra:

public class Compra
    {
        public virtual int Id { get; set; }
        public virtual string Tipo { get; set; }
        public virtual float VlrUnit { get; set; }
        public virtual float VlrTotal { get; set; }
        public virtual int Quant { get; set; }
        public virtual string NomeProduto { get; set; }
        public virtual DateTime DtCompra { get; set; }
        public virtual Fornecedores Nome { get; set; }
    }

Model Abastecimento:

public class Abastecimento
{
    public virtual int Id { get; set;}
    [Required]
    public virtual int Litro { get; set; }
    public virtual DateTime? DtAbastecido { get; set; }
    public virtual float VlrUnit { get; set; }
    public virtual int Km { get; set; }
    public virtual float TotalGasto { get; set; }
    public virtual Usuario Autor { get; set; }
    public virtual Compra NomeProduto { get; set; }
    public virtual Veiculo NumCarro { get; set; }
}

Controller:

public ActionResult Adiciona(AbastecimentoModel viewModel)
    {
        var Produto = ckm.ConsultaProduto(viewModel.NomeProdutoId);

        /*Aqui na verdade é um teste, mas o objetivo dele seria para pegar os valores que tem na tabela compra, e então utilizar na variavel Valor, para ordernar*/
        var teste = ckm.ConsultaValor(compra.VlrUnit);

        var Valor = Produto.OrderByDescending(a => teste).Last();

        viewModel.TotalGasto = viewModel.Litro * viewModel.VlrUnitId;

        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);
        }

    }

Query:

public IList<Compra> ConsultaValor(float VlrUnit)
    {
        string hql = "SELECT c FROM Compra c";
        IQuery query = session.CreateQuery(hql);
        return query.List<Compra>();
    }

Tabela Compra: inserir a descrição da imagem aqui

  • 1

    "the problem that returns value, zero or else value of another variable". Can you explain it a little better? It’s kind of confusing.

  • @Marconciliosouza I don’t understand so much of c# with nhibernate, but in another form, I was able to use this way, where the select took all the information and the controller could organize

  • @LINQ do not know if improved, but it would be basically where I try to pull the value and it does not pull, so not registering anything

  • Buddy, you still don’t know what you really need, or don’t know how to expose it here. Do not give to elaborate an answer without you put your classes , and say what you expect from your selects.

  • @Marconciliosouza I think it’s now clearer, and the models have been posted to shows where you have keys and which the key

  • what is the relationship between the product table and purchase ? post the Product also

  • There is no table Produto, the product I say, is when you register in the table Compra, every product that existed and will exist is in the table Compra

Show 2 more comments

1 answer

2


Try changing your method to;

public Compra ConsultaValor()
{
    return Session.Query<Compra>()
                .OrderByDescending(x => x.DtCompra)
                .FirstOrDefault();
}

And on the call

var utimaCompra = ckm.ConsultaValor();

Abastecimento abastecimento = viewModel.CriaAbastecimento();

abastecimento.VlrUnit = utimaCompra.VlrUnit;
abastecimento.NomeProduto = utimaCompra.NomeProduto;
  • Okay, and for you to use the a => teste no order by ? would not be a Where(p => p.compra == teste.compra) ? something else ... the Orderbydescending` makes a decreasing ordination and the . Last(); to take the last value of deregulation, that is... would be the first object without ordering.

  • Come on, what really has to be returned from your method Query value ? which parameter does it have to receive ? will it use it for what? Edit in your reply.....

  • Marco, the code worked, but it’s either he’s taking the last value or the first, depending on the way I put the OrderBy, there would be no way for him to take the value according to the product selected?

  • Yes, you have the product id ? if had you can pass as parameter for your query and use Where(x => x.idproduct == id) something like this.

  • I have Id yes, I will implant the line in the parameter.

Browser other questions tagged

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