Search Object By date and Id Lambda, Linq, asp.net

0

I have class called VehicleSequence, a class called VehicleMovement and the Vehicle class.

     class RequisicaoDeVeiculo
        {
            public int Id { get; set; }
            public DateTime DataDaSolicitacao { get; set; }
[ForeignKey("Veiculo")]
        public int? IdVeiculo { get; set; }
        public virtual Veiculo Veiculo { get; set; }



        [ForeignKey("Motorista")]
        public int? IdMotorista { get; set; }
        public virtual Motorista Motorista { get; set; }

        [ForeignKey("Mv")]
        public int? IdMv { get; set; }
        public virtual Mv Mv { get; set; } --> Abreviação de Movimentação de Veículo


        [ForeignKey("Usuario")]
        public int IdUsuario { get; set; }
        public virtual Usuario Usuario { get; set; }

        }


public class MovimentacaoDeVeiculo
{
    public int Id { get; set; }
    public DateTime DataDaChegada { get; set; }
}

I'm trying to get a request, like this: Suppose a person has a ticket for a specific vehicle, the fine is xx / xx / xxxx

I have a method to fetch the request, by the request date and vehicle id, but it's not working, I'm using Lambda. Can anyone give me a tip?

public ActionResult BuscarSolicitacaoPordata(DateTime? data, int? Idveiculo)
    {
        RequisicaoDeVeiculo requisicao = new RequisicaoDeVeiculo();
        if (data == null && Idveiculo == null)
        {
            ViewBag.IdVeiculo = new SelectList(db.VeiculoDb, "Id", "Descricao", RequisicaoDeVeiculo.IdVeiculo);
            return View();
        }
        ViewBag.IdVeiculo = new SelectList(db.VeiculoDb, "Id", "Descricao", vrs.IdVeiculo);

        var resultado = db.RequisicaoDeVeiculoDb.FirstOrDefault(v => v.DataEHoraServico >= data && v.MovimentacaoDeVeiculo.DataChegada <= data && v.IdVeiculo == Idveiculo);

        if (resultado == null)
        {
            return RedirectToAction("BuscarSolicitacaoPordata", "Vr").Mensagem("Nenhum Resultado Encontrado!", "Ops!");
        }

        return View(resultado);
    }
    
asked by anonymous 28.08.2018 / 02:04

1 answer

0

You forgot to include the Vehicle Movement data to your Vehicle Requisition. In addition, the DataEHoraServico property is not part of the class you have posted. Make sure the name of the ServiceDate and try this:

var resultado = db.RequisicaoDeVeiculoDb.Include("MovimentacaoDeVeiculo").FirstOrDefault(v => v.DataEHoraServico >= data && v.MovimentacaoDeVeiculo.DataChegada <= data && v.IdVeiculo == Idveiculo);
    
11.09.2018 / 22:05