So I´m having a problem with a query im trying to do. I´m trying to get the last element of a list in a linq query in c# using ef.
Here is the code im trying to execute:
public List<Viagem> getViagensToBeCompletedInBlocoTrabalho()
{
return this.context.Viagens
.Where(v => v.blocosTrabalho.Count() == 0
|| !v.blocosTrabalho.LastOrDefault().noFim.Equals(v.horasPassagem.LastOrDefault().codigoNo)
)
.ToList();
}
But i get this error: Queries performing 'LastOrDefault' operation must have a deterministic sort order. Rewrite the query to apply an 'OrderBy' clause on the sequence before calling 'LastOrDefault'.
What am i doing wrong here?
By the way the sorting that i want is the order of entries in the table so i don´t really want to sort anything. Although i have also tested sorting and it doesn´t work.
Thanks for the help in advance :-)