I know that Entity framework doesn't support sort or filter in the children collections yet. What I thought is that first I get the data then use foreach loop to sort it. The result gives me an unsorted list. My goal is to get the Participants (any order) and CurrentHospitaliztions (order by id descending) which is a child of participants. The models and the query is below. Any help will be appriciated.
public class Participant
{
public int Id { get; set; }
.. other fields
public ICollection<CurrentHospitalization> CurrentHospitalizations { get; set; }
public Participant()
{
CurrentHospitalizations = new Collection<CurrentHospitalization>();
}
}
public class CurrentHospitalization
{
public int Id { get; set; }
.. other fields
public Participant Participant { get; set; }
public int ParticipantId { get; set; }
}
The query that I use:
public async Task<IEnumerable<Participant>> GetList()
{
var participants = await context.Participants
.Include(x => x.CurrentHospitalizations)
.ToListAsync();
foreach (var p in participants )
{
var s = p.CurrentHospitalizations;
foreach (var q in s)
{
s.OrderByDescending(u => u.Id);
}
}
return participants ;
}