1
I’m learning a little about LAMBDA and tried to convert this query into LAMBDA, but I stopped.
I have 2 classes:
public class Voo {
public string idVoo {get;set;}
public DateTime dtVoo {get;set;}
public string dsHoraInicial {get;set}
public DateTime dtVooFim {get;set;}
public string dsHoraFinal {get;set}
public int idAeronave {get;set;}
public virtual Aeronave Aeronave {get;set;}
}
public class Aeronave {
public int idAeronave {get;set;}
public string dsAeronave {get;set;}
public string cdAeronave {get;set;}
public virtual ICollection<Voo> Voos {get;set;}
}
The user must inform a certain period with 2 dates as input and the query would make a grouping of cdAeronaves (aircraft code) and total flight hours performed by aircraft with that code.
Aeronave Horas de Voo
----------------------
PR-QQ 0,7
PR-UU 2,9
Below follows the process I have to use to adapt in LAMBDA.
ALTER PROCEDURE
@dtInicial DATETIME,
@dtFinal DATETIME
AS
BEGIN
SELECT a.cdAeronave 'dsAeronave', a.idAeronave
,SUM(CONVERT(numeric(10,1),DATEDIFF(minute,Convert(DATETIME, v.dtVoo+v.dsHoraInicial), Convert(DATETIME, v.dtVooFim+v.dsHoraFinal))/60.2)) AS 'HDV'
FROM Voo v
JOIN Aeronave a ON a.idAeronave = v.idAeronave
WHERE v.dtVoo BETWEEN @dtInicial AND @dtFinal
GROUP BY a.cdAeronave, a.idAeronave
END
And below the attempt of the LAMBDA.
IQueryable<Voo> selecionar = hdva.Include(a => a.Aeronave)
.Where(v => v.dtVoo <= Convert.ToDateTime(v.dtVoo + v.dsHoraInicial))
.Where(v => v.dtVoo >= Convert.ToDateTime(v.dtVooFim + v.dsHoraFinal))
.Select(s => new
{
idAeronave = s.Aeronave.idAeronave,
dsAeronave = s.Aeronave.cdAeronave,
hdv = Sum(Convert.ToDecimal
(
DbFunctions.DiffMinutes
(
Convert.ToDateTime(q.dtVoo + q.dsHoraInicial),
Convert.ToDateTime(q.dtVooFim + q.dsHoraFinal)
) / 60
)
)
})
.GroupBy(a => a.Aeronave.cdAeronave, a => a.Aeronave.idAeronave.ToString());
I know the SUM is wrong and that the 'q'(The name q does not exist in the current context)
inside the SUM is not being called, but I’m not sure how to make it all work.
Joao, in fact, I tested and still had problems with the sum, but thank you very much for the help!
– Rodrigo Ferraz
If the answer was useful to you, give an UP! But by the way, what error gives you?
– João Martins
Sorry. Error says that Sum is not a Flight method since it belongs to Flight class. It would only work if it was a Ienumerable, where this method could be invoked. Another error is the Unexpected Character $.
– Rodrigo Ferraz
Answer changed to avoid errors. Try to run as you are now.
– João Martins
I’ve checked flights and aircraft like this
ICollection<Voo> voos = db.Voos.ToList();
ICollection<Aeronave> aeronaves = db.Aeronaves.ToList();
I modified thewhere
for that reasonvoo.dtVoo >= DateTime.Parse(string.Format(@"{0} {1}", viewModel.dtVoo.ToShortDateString(), voo.dsHoraInicial))
 && viewModel.dtVoo <= DateTime.Parse(string.Format(@"{0} {1}", viewModel.dtVooFim.ToShortDateString(),voo.dsHoraFinal))
lista
doesn’t work when I put it onforeach
after a long time waiting.– Rodrigo Ferraz
@Rodrigoferraz, I changed my answer considerably and included examples. I hope it helps!
– João Martins
Martins, this code returned all the aircraft, even those without Flight, in any time interval informed, because the Where was missing. I’ll try to implement that part, but thank you very much!
– Rodrigo Ferraz
I could only explain, please, why you included MAX?
– Rodrigo Ferraz
Let’s go continue this discussion in chat.
– João Martins