1

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 :-)

3
  • You don't say which DBMS you are using. But this doesn't really make sense "the order of entries in the table" in most systems. Tables have no defined order as such Commented Dec 31, 2020 at 3:08
  • Can you share the code of testing with sorting? You face the same problem with sorting? Commented Dec 31, 2020 at 5:29
  • @ChetanRanpariya not quite, it was a different one but still didnt work maybe i contructed badly the query. Commented Dec 31, 2020 at 10:52

1 Answer 1

2

Actually SQL standard do not support retrieving records from the end and even do not guarantee records in concrete order. EF is trying to simulate that by correcting OrderBy operator and if you do not specify that - it will fail.

Consider to rewrite your query and make it performant:

public List<Viagem> getViagensToBeCompletedInBlocoTrabalho()
{ 
    var query =
       from v in this.context.Viagens
       from b in v.blocosTrabalho.OrderByDescending(x => x.Id)
          .Take(1).DefaultIfEmpty()
       from h in v.horasPassagem.OrderByDescending(x => x.Id)
          .Take(1).DefaultIfEmpty()
       where
          b == null || b != null && b.noFim == h.codigoNo
       select v;

     return query.ToList();
 }
Sign up to request clarification or add additional context in comments.

3 Comments

That works but only if the blocoTrabalho Id is sequencial. This id the user can specify so for example if the first blocotrabalho has id of BT2 and the second BT1, this will not work. I need to get the list ordered by the entry in the table. HorasPassagem is fine though i can order it, no problems there!
If you need such query, you have to introduce sequence somehow. Or choose several columns for ordering. Database engine do not guarantee records order if you do not specify OrderBy.
Sorry nevermind the comment i was missing a concept in the domain i can actually get everything ordered thank so much =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.