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

Asked

Viewed 112 times

0

I have class called Requisicaodeveiculo, a class called Moving vehicle and class Vehicle.

     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 am trying to seek a request as follows: Let’s suppose that one arrived a fine for a particular vehicle, the fine ta on the date xx/xx/xxxx

I have a method to find the request, by the date of the request and vehicle id, but it’s not working, I’m using Lambda. Can someone give me a hint?

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);
    }
  • Where the property comes from DataEHoraServico? The estate DataChegada shouldn’t be DataDaChegada? What is being returned?

  • DataEHoraServico,comes from the class RequisicaoDeVeiculo.Arrival date, comes from class MovimentacaoDeVeiculo, I tried to use it in a different way. so: var r = db.VrDb.Where(x => x.DataEHoraServico == data && x.Mv.DataChegada <= data).ToList(); this line of code returns me all requests whose date and service is greater than or equal to the date chosen in the view, and that the date of arrival is less than or equal to the date coming from the view,

  • But you managed to solve your problem?

  • the problem is the following, let’s assume that a vehicle went out for mission day the 26/06/2018, and returned day 30/06/2018. dai arrived a fine for this vehicle on the date of 28/06/2018. i’m wondering, who was driving this vehicle, using the date of the fine, and the vehicle id.

  • After listing all I use this expression var v = r.FirstOrDefault(m => m.IdVeiculo == Idveiculo);

  • But then with your code you can return what you want, right?

  • No, when I use var r = db.VrDb.Where(x => x.DataEHoraServico == data && x.Mv.DataChegada <= data).ToList(); he brings me nothing. and when I use var r = db.VrDb.Where(x => x.DataEHoraServico == data || x.Mv.DataChegada <= data).ToList(); he brings me, but not always the right vehicle

  • If you have the IdVericulo you will need to use it in the query, otherwise it will return you results from other vehicles. The property DataEHoraServico has hour, minute and second?

  • It does have the date and time the vehicle went on mission, and the date and time the vehicle arrived from the mission

Show 4 more comments

1 answer

0

You forgot to include Vehicle Movement data in your Vehicle Requisition. Also, the Dataehoraservico property is not part of the class you posted. Make sure the Dataehoraservico name and try this:

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

Browser other questions tagged

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