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 estateDataChegada
shouldn’t beDataDaChegada
? What is being returned?– João Martins
DataEHoraServico
,comes from the classRequisicaoDeVeiculo
.Arrival date, comes from classMovimentacaoDeVeiculo
, 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,– Rafael Passos
But you managed to solve your problem?
– João Martins
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.
– Rafael Passos
After listing all I use this expression
var v = r.FirstOrDefault(m => m.IdVeiculo == Idveiculo);
– Rafael Passos
But then with your code you can return what you want, right?
– João Martins
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 usevar r = db.VrDb.Where(x => x.DataEHoraServico == data || x.Mv.DataChegada <= data).ToList();
he brings me, but not always the right vehicle– Rafael Passos
If you have the
IdVericulo
you will need to use it in the query, otherwise it will return you results from other vehicles. The propertyDataEHoraServico
has hour, minute and second?– João Martins
It does have the date and time the vehicle went on mission, and the date and time the vehicle arrived from the mission
– Rafael Passos