简体   繁体   中英

LINQ to sort a List of Objects

I'm having a List<Clinics> which has a property (another class) called List<Doctors> . Each Doctors has a property called AverageCost . I want to create a LINQ to sort the List<Clinics> based on Lowest average cost across all the Doctors and ThenBy ClinicID .
I've tried using

clinics = clinics.OrderBy(f => {
    f.Doctors = f.Doctors.OrderBy(p => p.AverageCost).ToList();
    return f;
})
.ThenBy(f => f.ClinicId).ToList();

And also

clinics = (from t in clinics select t).OrderBy(x => x.Doctors.OrderBy(z => z.AverageCost)).ToList();

But nothing seems to work. Please advise where I'm wrong. Thanks

The reason that your implementations do not work is that OrderBy expects each element to return a single "thing" by which the elements could be compared. Your implementations return lists, which do not have a built-in way to be compared to each other.

You don't need to order doctors by average cost, just pick the lowest cost using Min() function:

clinics = clinics
    .OrderBy(f => f.Doctors.Min(d => d.AverageCost))
    .ThenBy(f => f.ClinicId)
    .ToList();

So every Clinic has zero or more Doctors. Every Doctor has a property AverageCost.

I want to sort the Clinics based on Lowest average cost across all the Doctors

Do you mean the lowest average of (average costs of the Doctor), or do you want to order by the Cheapest Doctor = the Doctor with the lowest average cost?

Clinic A:
   Doctor A1: Average cost 10
   Doctor A2: Average cost 50
Clinic B:
   Doctor B1: Average cost 25
   Doctor B2: Average cost 15

Doctor A1 is the cheapest doctor of ClinicA with an average cost of 10. Doctor B2 is the cheapest doctor of ClinicB with an average cost of 25.

If sorting on "Lowest average cost across all the Doctors" means that you want to sort by the cheapest doctor, you get Clinic A followed by Clinic B

If "Lowest average cost across all the Doctors" means the average of the AverageCosts per Clinic, you get that ClinicA has Average costs over all doctors (10+50)/2 = 30; ClinicB has average costs over all doctors of (25+15)/2 = 20. You'd take first ClinicB, then ClinicA

So which one do you want?

Order by the cheapest Doctor

var result = Clinics.Select(clinic => new
{
     CheapestDoctor = clinic.Doctors
          .OrderBy(doctor => doctor.AverageCost)
          .FirstOrDefault(),
     Clinic = clinic,
})
.OrderBy(clinic => clinic.CheapestDoctor.AverageCost)
.Select(clinic => clinic);

(TODO: decide what to do with Clinics without doctors)

Order by overall lowest Average

var result = Clinics.Select(clinic => new
{
     OverallAverage = clinic.Doctors
          .Select(doctor => doctor.AverageCost)
          .Average(),
     Clinic = clinic,
})
.OrderBy(clinic => clinic.OverallAverage)
.Select(clinic => clinic);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM