I`m novice in programming, looking for any advise.
I have a list of patients in clinic, each patient has one of these type: "child", "teenager", "adult", "oldage" and also has the date of the visit to the doctor.
The issue to get from list of patients only four patients, one patient of each type and has the latest date of the last doctor's visit.
I can do this by four separate request to db.
var childPatient = await db.patientsOfClinic.Where(x => x.TypeOfPatient == "child").OrderByDescending(x => x.UploadedDate).FirstOrDefaultAsync();
But need to handle it by one request to db.
Tried such request, but it incorrect.
var patientsOfClinic = await db.patientsOfClinic.Where(x => x.ClinicId == clinicId && x.TypeOfPatient == "child" && x.TypeOfPatient == "teenager" && x.TypeOfPatient == "adult" && x.TypeOfPatient == "oldage").OrderByDescending(y => y.UploadedDate).ToListAsync();
Thanks for any advise.
What you can do is the following:
First, sort the patients by the upload date.
Then group them by the type. you will get 4 groups ordered by date.
The from each group, take the first patient which is also latest.
This will yield a list of 4 patients, one of each group, with the latest UploadDate.
var patients = db.patientsOfClinic.OrderByDescending(patient => patient.UploadDate)
.GroupBy(patient => patient.TypeOfPatient)
.Select(g => g.FirstOrDefault());