Put the value of a hql in a var [closed]


I need to have my two tables work together, where Abastecimento takes the Product Name ( NomeProdutoId ), from the Compra table, I was able to get it to get the Product Name, but wanted me to If you do not know the name of the product, you will have to enter the value of that product, and register in the Abastecimento table, an example would be those registration forms, where you inform your state and just below you only bring cities from that state.

But in my case I wanted the value, and the last registered value, of that product.

This part was an attempt where in another form I 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 a foreign key, that's fine.

Model Buy:

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 Supply:

public class Abastecimento
    public virtual int Id { get; set;}
    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; }


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();
            //return View();
            return RedirectToAction("Index");
            ViewBag.Compra = compraDAO.Lista();
            ViewBag.Usuarios = usuarioDAO.Lista();
            ViewBag.Veiculo = veiculoDAO.Lista();
            return View("Form",viewModel);



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

Purchase Table:

asked by anonymous 23.10.2017 / 19:35

1 answer


Try changing your method to;

public Compra ConsultaValor()
    return Session.Query<Compra>()
                .OrderByDescending(x => x.DtCompra)

And in the call

var utimaCompra = ckm.ConsultaValor();

Abastecimento abastecimento = viewModel.CriaAbastecimento();

abastecimento.VlrUnit = utimaCompra.VlrUnit;
abastecimento.NomeProduto = utimaCompra.NomeProduto;
23.10.2017 / 20:17