检查列表对象是否具有特定值

时间:2019-11-03 13:05:38

标签: c# .net

我有doctorpatient类型的列表。 我需要一种遍历列表并获取所有已由特定doctor治疗过的患者的方法。

//Appointment objects
Appointment app1= new Appointment(doctor1, patient1);
Appointment app2= new Appointment(doctor2, patient3);
Appointment app3= new Appointment(doctor1, patient2);
Appointment app4= new Appointment(doctor3, patient4);
Appointment app1= new Appointment(zdravnik2, pacient4);
Appointment app1= new Appointment(zdravnik3, pacient2);
Appointment app1 = new Appointment(zdravnik3, pacient5);
Appointment app1 = new Appointment(zdravnik1, pacient6);
Appointment app1 = new Appointment(zdravnik2, pacient4);
Appointment app1 = new Appointment(zdravnik1, pacient4);

//The objects are stored in this list, they are added in the   constructor
List<Appointment>AllAppointments=new List<Appointment>();

//Gets all the patients that have been treated by a particular doctor
public List<Patient> HadAppointmentWith(Doctor d)
{
    //Dont know how to continue from here on
    //var find=AllAppointment.GroupBy(x=>x.Patient)
}

1 个答案:

答案 0 :(得分:0)

您可以使用Linq扩展方法:

using System.Linq;

public List<Patient> GetPatients(Doctor doctor)
{
  return AllAppointments
         .Where(appointment => appointment.Doctor == doctor)
         .Select(appointment => appointment.Patient)
         .Distinct()
         .ToList();
}

它过滤完整列表,以使所有与所需医生匹配的医生。

接下来,它将选择该医生的所有患者。

然后在重复的情况下选择不同的患者。

然后我们将Linq查询的IEnumerable<>序列结果转换为List<Patient>,以返回方法。

相关问题