Error trying to register

0

Good afternoon, at the time I try to register in the form, I program in C # language with asp.net, razor and NHibernate.

This is my DAO

public IList<Rota> Comparacao()
        {
        //*string hql = "Select p from Rota p";
        string hql = "SELECT Id, DtLancamento, Km_Atual, Combustivel, NVeiculoId, (SELECT TOP 1 x.Km_Atual FROM Rota x WHERE x.NVeiculoId  = r.NVeiculoId AND x.Id != r.Id AND x.DtLancamento < r.DtLancamento Order By x.DtLancamento desc, x.Id desc) as Km_Anterior FROM Rota r";
        IQuery query = session.CreateSQLQuery(hql);
        return query.List<Rota>();
        }

My Controller

dao.Comparacao();
            if (viewModel.Km_Atual < p.Km_Atual)
                    {
                    ModelState.AddModelError("Km_Atual.Invalido",
                    "Km Atual precisa ser maior que o atual");
                    }

                if (ModelState.IsValid)
                    {
                    Rota rota = viewModel.CriaRota();
                    dao.Adicionar(rota);
                    //return View();
                    return RedirectToAction("Form");
                    }
                else
                    {
                    ViewBag.Veiculo = veiculoDao.Lista();
                    return View("Index", viewModel);
                    }
                }

My Model

public class Rota
        {
        public virtual int Id { get; set; }
        public virtual DateTime DtLancamento { get; set; }
        public virtual int Km_Atual { get; set; }
        public virtual int Km_Anterior { get; set; }
        public virtual float Combustivel { get; set; }
        public virtual Veiculo NVeiculo { get; set; }
        }
    }

My ViewModel

public class RotaModel
    {
    public int Id { get; set; }
    public DateTime DtLancamento { get; set; }
    public int Km_Atual { get; set; }
    public int Km_Anterior { get; set; }
    public float Combustivel { get; set; }
    public int NVeiculoId { get; set; }

    public Rota CriaRota()
        {
        Rota rota = new Rota()
        {
            Id = this.Id,
            DtLancamento = this.DtLancamento,
            Km_Atual = this.Km_Atual,
            Km_Anterior = this.Km_Anterior,
            Combustivel = this.Combustivel
        };

        if (this.NVeiculoId != 0)
        {
            Veiculo ncarro = new Veiculo()
        {
            Id = this.NVeiculoId
        };
            rota.NVeiculo = ncarro;
        }
        return rota;
        }

    public RotaModel(Rota p)
        {
        this.Id = p.Id;
        this.DtLancamento = p.DtLancamento;
        this.Km_Atual = p.Km_Atual;
        this.Km_Anterior = p.Km_Anterior;
        this.Combustivel = p.Combustivel;
        if (p.NVeiculo != null)
            {
            this.NVeiculoId = p.NVeiculo.Id;
            }
        }
    public RotaModel()
    {
    //* Construtor vazio, sempre necessario!! <-- Importante.    
    }
    }
}

The error that appears when I try to register is this: The value "System.Object []" is not of type "BlogWeb.Models.Rota" and can not be used in this generic collection.     

asked by anonymous 04.07.2017 / 21:33

1 answer

0

Try to transform the columns to the Rota class using Transform from nhibernate , like this:

IQuery query = session.CreateSQLQuery(hql).TransformUsing(Transformers.AliasToBean<Rota>());
    
05.07.2017 / 02:30