I have the following query:
query = from p in db.pessoa
join f in db.pessoa_origem on p.Pessoa_Origem_Id equals f.Id
join s in db.pessoa_status on p.Pessoa_Status_Id equals s.Id
join c in db.contato on p.Id equals c.Pessoa_Id
select p;
And I want to sort by c.data
.
The cardinality of the relationship between person and contact is 1: n. That is, a person can have 0 or n contacts.
When I use the code:
query = query.OrderByDescending(u => u.contato.Count > 0 ? u.contato.OrderByDescending(t => t.Id).FirstOrDefault().Data : null);
It works, however, it is getting the highest% of% of contacts to sort by its date. What I need is to get the contact with the highest date and sort by date. However, the following snippet does not work:
query = query.OrderByDescending(u => u.contato.Count > 0 ? u.contato.OrderByDescending(t => t.Data).FirstOrDefault().Data : null);
Can anyone help with how to solve this problem?